android 蓝牙4.0 ble 的理解

环境:android 4.3 api18
参考:官方的BluetoothleGatt sample 和xpg的GizwitsBLE
记录核心的内容,憧憬物联网

权限:
<span style="font-size:18px;"><uses-permission android:name="android.permission.BLUETOOTH"/>
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>

<uses-feature android:name="android.hardware.bluetooth_le" android:required="true"/>

android:minSdkVersion="18"</span>

scan:

<span style="font-size:18px;">// 获取到蓝牙适配器
final BluetoothManager bluetoothManager =
                (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
mBluetoothAdapter = bluetoothManager.getAdapter();

// 适配器搜索的回调
private BluetoothAdapter.LeScanCallback mLeScanCallback =
            new BluetoothAdapter.LeScanCallback() {

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

// 使用适配器进行搜索
mBluetoothAdapter.startLeScan(mLeScanCallback);

// 停止搜索
mBluetoothAdapter.stopLeScan(mLeScanCallback);</span>

BluetoothLeService: 里面包含了所有对ble设备的操作

onUnbind: 
<span style="font-size:18px;">mBluetoothGatt.close();</span>

connect:
<span style="font-size:18px;">// 真正连接设备的地方,这里不能重复调,要确保调用了mBluetoothGatt.close();才能调用下面的connectGatt
mBluetoothGatt = device.connectGatt(this, false, mGattCallback);</span>

disconnect:
<span style="font-size:18px;">mBluetoothGatt.disconnect();</span>

close:
<span style="font-size:18px;">mBluetoothGatt.close();</span>

readCharacteristic:
<span style="font-size:18px;">// 读service里面的characteristic,ble设备会发送信息到某个的UUID,
mBluetoothGatt.readCharacteristic(characteristic);</span>

writeCharacteristic:
<span style="font-size:18px;">// 写信息给设备
mBluetoothGatt.writeCharacteristic(characteristic);</span>

setCharacteristicNotification:
<span style="font-size:18px;">// 使能某个UUID地址,一旦这个地址有数据的变化就会通知我们
mBluetoothGatt.setCharacteristicNotification(characteristic, enabled);</span>

BluetoothGattCallback:
<span style="font-size:18px;">// ble的回调,当ble有什么动作的时候都到这里来,所有的交互信息
onConnectionStateChange
onServicesDiscovered
onCharacteristicRead
onCharacteristicChanged
onCharacteristicWrite</span>

control:
<span style="font-size:18px;">// 广播处理service传出来的数据交互信息
BroadcastReceiver
ACTION_GATT_CONNECTED
ACTION_GATT_DISCONNECTED  // 这里要注意调用mBluetoothLeService.close();让disconnected后可以重新connect
ACTION_GATT_SERVICES_DISCOVERED  // 发现到ble设备的sevices的时候,调用使能语句                            
                                 //  setCharacteristicNotification(xx, true)使能某个UUID
ACTION_DATA_AVAILABLE // 当这个使能的通道数据有变化的时候,就会调用这里
</span>

写信息给设备:
<span style="font-size:18px;">// 获取指定Service里的指定Characteristic, 然后在这个Characteristic上把信息写进去
BluetoothGattCharacteristic characteristic = mBluetoothLeService.getService(
                        SERVICE_UUID).getCharacteristic(UUID);    
characteristic.setValue(value);
mBluetoothLeService.writeCharacteristic(characteristic);</span>

注意的地方:
1. 发送和接收的UUID可以是不同的,这个要跟设备厂商协调
2. 发送数据的指令和格式,即协议,是设备厂商定的,需要文档支持
3. 不用的时候记得调用close(), 不让就会发现蓝牙搜索不到设备的情况。可以让连接的手机重启或清理缓存等操作解决

最后附上自己的理解,也许不太对:
ble就是一堆作为server的设备,把信息散发出来在某些通道,然后用手机等东西作为client去连接他们,交互发生在这些通道上,一个server只能被一个client连接

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值