找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 1900|回复: 0
收起左侧

关于android APP 与 蓝牙连接的问题

[复制链接]
ID:84283 发表于 2017-12-25 15:27 | 显示全部楼层 |阅读模式
// 蓝牙设备连接程序
    private class BluetoothConnect extends Thread
    {
        private BluetoothSocket tempSocket;
        private BluetoothDevice tempDevice;

        public BluetoothConnect(BluetoothDevice device)
        {
            BluetoothSocket temp = null;
            tempDevice = device;

            //以下程序用于获取BluetoothSocket
            try
            {
                temp = tempDevice.createRfcommSocketToServiceRecord(UUID.fromString(BluetoothCOM_UUID));

                Toast.makeText(getApplication(), device.getAddress(), Toast.LENGTH_LONG).show();
            } catch (IOException e) {

                e.printStackTrace();
            }

            tempSocket = temp;
        }
        public void run()
        {
            myBluetoothAdapter.cancelDiscovery();
            //以下程序用于连接蓝牙设备
            try
            {
                tempSocket.connect();
//                MainActivity.myBluetoothSocket=x;//此处注意

                Message msg = new Message();
                msg.arg1 = 1;
                handler.sendMessage(msg);
            }
            catch (IOException e)
            {
                try
                {
                    tempSocket.close();
                    Message msg = new Message();
                    msg.arg1 = 2;
                    handler.sendMessage(msg);
                }
                catch (IOException ee)
                {

                }
                return;
            }


        } 连接原理,到底是怎么回事?
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表