当前位置: 移动技术网 > 移动技术>移动开发>Android > Android 蓝牙2.0的使用方法详解

Android 蓝牙2.0的使用方法详解

2019年07月24日  | 移动技术网移动技术  | 我要评论

本文为大家分享了android操作蓝牙2.0的使用方法,供大家参考,具体内容如下

1.android操作蓝牙2.0的使用流程
(1)找到设备uuid
(2)获取蓝牙适配器,使得蓝牙处于可发现模式,获取下位机的socket,并且与上位机建立建立连接,获取获取输入流和输出流,两个流都不为空时,表示连接成功。否则是连接失败。
(3).与下位机的socket开始通信。
(4).通信结束后,断开连接(关闭流,关闭socket)

2接下来接直接上代码:
2.1找到设备uuid(一般厂商都会给开发者提供)

复制代码 代码如下:
uuid my_uuid = uuid.fromstring("00001101-0000-1000-8000-00805f9b34fb"); 

2.2与蓝牙设备建立连接

bluetoothadapter mybluetoothadapter = null;//蓝牙适配器 
bluetoothserversocket mbthserver = null;//上位机<span style="font-family: arial, helvetica, sans-serif;">serversocket</span> 
bluetoothsocket mbthsocket = null;//下位机的socket 
inputstream mminstream = null;//输入流 
outputstream mmoutstream = null;//输出流 
<span style="font-family: arial, helvetica, sans-serif; background-color: rgb(255, 255, 255);">    </span>
mybluetoothadapter = bluetoothadapter.getdefaultadapter();//获取适配器 
set<bluetoothdevice> paireddevices = mybluetoothadapter 
    .getbondeddevices();//获取适配器下的所有蓝牙设备 
if (paireddevices.size() > 0) { 
  for (iterator<bluetoothdevice> iterator = paireddevices 
      .iterator(); iterator.hasnext();) { 
    bluetoothdevice device = (bluetoothdevice) iterator 
        .next(); 
    if (device_name1.equals(device.getname()) 
        || device_name2.equals(device.getname()) 
        || device_name3.equals(device.getname()) 
        || device_name4.equals(device.getname())) { 
      try { 
        mybluetoothadapter.enable();//将适配器设置可用 
        intent discoverableintent = new intent( 
            bluetoothadapter.action_request_discoverable);// 使得蓝牙处于可发现模式,持续时间150s 
        discoverableintent 
            .putextra( 
                bluetoothadapter.extra_discoverable_duration, 
                150); 
        mbthsocket = device 
            .createrfcommsockettoservicerecord(my_uuid);//获取下位机的socket 
 
        int sdk = integer.parseint(build.version.sdk); 
        if (sdk >= 10) { 
          mbthsocket = device 
              .createinsecurerfcommsockettoservicerecord(my_uuid); 
        } else { 
          mbthsocket = device 
              .createrfcommsockettoservicerecord(my_uuid); 
        } 
 
        mbthserver = mybluetoothadapter 
            .listenusingrfcommwithservicerecord( 
                "myserversocket", my_uuid);监听可用的设备 
        mbthsocket.connect(); // 建立连接 
        mminstream = mbthsocket.getinputstream();// 获取输入流 
        mmoutstream = mbthsocket.getoutputstream();// 获取输出流 
 
      } catch (ioexception e) { 
        ett.settext("设备连接异常!"); 
      } 
      if ((mminstream != null) && (mminstream != null))// 二者不为空时,表示连接成功,否则连接失败 
      { 
        ett.settext("设备连接成功!"); 
      } else { 
        ett.settext("设备连接失败!"); 
      } 
      break; 
    } 
  } 
} 

2.3开始发送数据,并且读取数据(字节数组)                    

 if ((mminstream == null) || (mminstream == null)) { 
  readflage = -2;// 连接异常 
  return; 
} 
mmoutstream.write(cmd_find);//写入查找指令 
thread.sleep(200); 
int datalen = mminstream.read(recdata);//读取数据 

 注意:cmd_find和recdata都是字节数组byte[].
以上代码就一次发送指令和读取数据的步骤。很简单吧

2.4断开连接

if ((mminstream == null) || (mminstream == null)) { 
  return; 
} 
//关闭流和socket 
mmoutstream.close(); 
mminstream.close(); 
mbthsocket.close(); 
mbthserver.close(); 

最后总结一下,基本就3大步,第一建立连接,第二发送数据读取数据,第三步断开连接。今天就这些了,以后会写关于蓝牙4.0 ble 在android中的使用,这两个还是有很多不同的,大家请期待。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网