android 蓝牙4.0读写,android蓝牙4.0的知识要点

8bb7c11a2112?t=1506224038007

蓝牙4.0

这次主要讲解蓝牙4.0的基本要点,作为自己的备忘录记录下来吧。首先普及一下蓝牙4.0基于Gatt协议来实现。而蓝牙4.0以下的是传统蓝牙,基于socket方式来实现。所以4.0以上的蓝牙具有传输速度更快,覆盖范围更广,安全性更高,延迟更短,耗电极低等等优点。

一个BLE终端可以包含多个Service, 一个Service可以包含多个Characteristic,一个Characteristic包含一个value和多个Descriptor,一个Descriptor包含一个Value。Characteristic是比较重要的,是手机与BLE终端交换数据的关键,读取设置数据等操作都是操作Characteristic的相关属性。

接下来就是代码部分:

1.首先是声明权限:

如果你想声明你的应用程序只能在支持BLE的设备上运行,可以将下面声明包含进你的应用程序manifest文件中:

2.其次获取蓝牙适配器:

BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();也可以用

BluetoothManager mBluetoothManager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);

BluetoothAdapter mBluetoothAdapter = mBluetoothManager.getAdapter();

当mBluetoothAdapter==null的时候就说明手机没有开蓝牙,此时我们可以通过调用系统的蓝牙打开窗口打开蓝牙,如下

Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);

startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);```

再通过

``` @Override

protected void onActivityResult(int requestCode, int resultCode, Intent data) {

super.onActivityResult(requestCode, resultCode, data);

}```

回调看时候开启成功。

3.接着就是查找蓝牙了。查找蓝牙很简单,首先就是定义蓝牙查找获取设备的回调接口,如下:

private BluetoothAdapter.LeScanCallback mLeScanCallback = new BluetoothAdapter.LeScanCallback() {

@Override

public void onLeScan(final BluetoothDevice device, int rssi, byte[] scanRecord) {

//device.getName();获取蓝牙设备名字

//device.getAddress();获取蓝牙设备mac地址

}

};然后使用mBluetoothAdapter .startLeScan(mLeScanCallback);开始搜索设备,每当有设备即通过回调onLeScan(final BluetoothDevice device, int rssi, byte[] scanRecord)方法来输出设备数据信息。当你不想再搜索是可以使用mBluetoothAdapter.stopLeScan(mLeScanCallback);```来停止搜索。

4.有蓝牙设备信息了,下一步我们当然要连接蓝牙了,不然要这些信息也没用了。连接蓝牙也是很简单。建议蓝牙连接最好在后台service进行。假如你保存上面回调方法的BluetoothDevice对象,就直接可以运行BluetoothGatt mBluetoothGatt= device.connectGatt(this, false, mGattCallback);这代码进行连接,至于mGattCallback是什么下文会介绍。而BluetoothGatt这对象也很重要,后面发现服务读写设备等操作都是通过该对象。假如没有BluetoothDevice 对象只有蓝牙设备的mac地址也可以连接,这个可以先像上面那样首先获取BluetoothAdapter蓝牙适配对象,BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(intent.getStringExtra("mac"));再通过getRemoteDevice()方法也可以BluetoothDevice 对象然后再像上面那样连接也可以。

上面连接代码中出现的mGattCallback对象,这个是什么呢?它是蓝牙连接,读取设备,往设备里写数据及设备发出通知等都会回调该接口方法,具体如下:

private final BluetoothGattCallback mGattCallback=new BluetoothGattCallback() {

//当连接上设备或者失去连接时会回调该函数

@Override

public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {

if(newState== BluetoothProfile.STATE_CONNECTED){

Log.e("log_state","连接成功");

mBluetoothGatt.discoverServices();

}else if(newState==BluetoothProfile.STATE_DISCONNECTED){

Log.e("log_state","连接失败");

}

super.onConnectionStateChange(gatt, status, newState);

}

//当设备是否找到服务时,会回调该函数

@Override

public void onServicesDiscovered(BluetoothGatt gatt, int status) {

super.onServicesDiscovered(gatt, status);

if (status == BluetoothGatt.GATT_SUCCESS) { //找到服务了

//在这里可以对服务进行解析,寻找到你需要的服务

}

}

//设备发出通知时会调用到该接口

@Override

public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) {

super.onCharacteristicChanged(gatt, characteristic);

Log.e("log_change","发送通知");

}

//当读取设备时会回调该函数

@Override

public void onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {

super.onCharacteristicRead(gatt, characteristic, status);

if (status == BluetoothGatt.GATT_SUCCESS) {

//读取成功

Log.e("log_read",characteristic.getValue()[0]+"");

}else{

//读取失败

}

}

//当向Characteristic写数据时会回调该函数

@Override

public void onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {

super.onCharacteristicWrite(gatt, characteristic, status);

if(status == BluetoothGatt.GATT_SUCCESS){

//写入成功

}else{

//写入失败

}

}

@Override //当向设备Descriptor中写数据时,会回调该函数

public void onDescriptorWrite(BluetoothGatt gatt,BluetoothGattDescriptor descriptor, int status) {

super.onDescriptorRead(gatt, descriptor, status);

}

};

这是整个蓝牙核心的回调方法,因为你所有的蓝牙操作都离不开这个方法。当我们调用connectGatt()方法进行连接,首先会回到onConnectionStateChange(BluetoothGatt gatt, int status, int newState);方法看是否已经连接成功,接入成功newState==BluetoothProfile.STATE_CONNECTED;此时我们就可以用 mBluetoothGatt.discoverServices();方法找出该设备中的服务了。当蓝牙设备服务查找完之后就会回调

onServicesDiscovered(BluetoothGatt gatt, int status);方法此时你就可以遍历出蓝牙设备的所有服务,例如方法如下:

private void displayGattServices(List gattServices) {

if (gattServices == null)

return;

for (BluetoothGattService gattService : gattServices) {

// 遍历出gattServices里面的所有服务

List gattCharacteristics = gattService.getCharacteristics();

for (BluetoothGattCharacteristic gattCharacteristic : gattCharacteristics) {

// 遍历每条服务里的所有Characteristic

if (gattCharacteristic.getUuid().toString().equalsIgnoreCase(需要通信的UUID)) {

// 有哪些UUID,每个UUID有什么属性及作用,一般硬件工程师都会给相应的文档。我们程序也可以读取其属性判断其属性。

// 此处可以可根据UUID的类型对设备进行读操作,写操作,设置notification等操作

// BluetoothGattCharacteristic gattNoticCharacteristic 假设是可设置通知的Characteristic

// BluetoothGattCharacteristic gattWriteCharacteristic 假设是可读的Characteristic

// BluetoothGattCharacteristic gattReadCharacteristic 假设是可写的Characteristic

}

}

}

}

到这一步,你就需要硬件工程师给你提供关于这个蓝牙设备的UUID文档,每个UUID的功能和操作都需要文档提供,不然你也不知道这些UUID具体代表什么功能和怎么用。当你知道UUID的意思,你就可以通过BluetoothGattCharacteristic 这个类进行各种读写操作。

5.当你从文档看到遍历出来的UUID有接送通知的功能。这时你就可以设置可以接收通知。代码如下:

public void setCharacteristicNotification(BluetoothGattCharacteristic characteristic, boolean enabled) {

if (mBluetoothAdapter == null || mBluetoothGatt == null) {

Log.w(TAG, "BluetoothAdapter not initialized");

return;

}

mBluetoothGatt.setCharacteristicNotification(characteristic, enabled);

BluetoothGattDescriptor descriptor = characteristic.getDescriptor(UUID.fromString("00002902-0000-1000-8000-00805f9b34fb"));

if (descriptor != null) {

descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);

mBluetoothGatt.writeDescriptor(descriptor);

}

}

通过拿到对应通知UUID的BluetoothGattCharacteristic,调用setCharacteristicNotification().其中00002902-0000-1000-8000-00805f9b34fb是系统提供接受通知自带的UUID,通过设置BluetoothGattDescriptor相当于设置BluetoothGattCharacteristic的Descriptor属性来实现通知,这样只要蓝牙设备发送通知信号,就会回调onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) 方法,这你就可以在这方法做相应的逻辑处理。

6。还是当你遍历的UUID服务中关于写数据到设备已达到控制设备的UUID是,你可以保存对应的BluetoothGattCharacteristic对象。然后向BluetoothGattCharacteristic对象写入数据,在通过

BluetoothGatt调用writeCharacteristic()方法即可向硬件写入数据,例如下代码:

sendCharacteristic.setValue(new byte[] {0x00});

mBluetoothGatt.writeCharacteristic(sendCharacteristic);

其中一般硬件里读出写入的数据为二进制类型,所以要熟悉整型,字符串,二进制,十六进制等它们之间的转换。至于写什么数据看硬件工程师的文档。

7.有写就有读,从蓝牙设备读数据也不难。首先还是从遍历的UUID中找到关于读取蓝牙设备数据的UUID,具体哪个UUID还是要看硬件文档。然后还是保存对应的BluetoothGattCharacteristic对象。当要读取时直接用运行BluetoothGatt的readCharacteristic(BluetoothGattCharacteristic characteristic);参数里的characteristic就是你保存的BluetoothGattCharacteristic对象,如mBluetoothGatt.readCharacteristic(getCharacteristic);然后就会回调上面的onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status);方法,最后当status == BluetoothGatt.GATT_SUCCESS时,即可通过characteristic.getValue();方法获取蓝牙设备返回的数据,你拿到数据剩下就是你的逻辑处理了。

至此,蓝牙4.0的关键知识就写完了,你掌握这些就可以连接蓝牙设备做很多事了,当然前提是要有蓝牙的硬件文档,不然你也不知道那些UUID是什么意思要怎么用。如果对你有帮助就请给我给喜欢吧,谢谢。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值