Android 蓝牙2.0的使用方法:

             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中的使用,这两个还是有很多不同的,大家请期待



                
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值