Android蓝牙BLE开发,扫描、连接、发送和读取信息;

1、BLE开发权限

Android蓝牙BLE开发须打开蓝牙权限和6.0位置权限;

    <uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
       if (Build.VERSION.SDK_INT < 23){return;}
//判断是否有权限
if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
//请求权限
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_COARSE_LOCATION}, 1);
}

若需要BLE支持feature 还需打开这个权限;

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

2、手机是否支持BLE,打开蓝牙;

先判断手机是否支持BLE,在获取蓝牙的适配器打开蓝牙;

获取蓝牙适配器和打开蓝牙各有两种方法,都可以;

    public void initialize(){
if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH_LE)) {
Toast.makeText(this, "BLE not supported", Toast.LENGTH_SHORT).show();
return;
}
BluetoothManager btManager = (BluetoothManager)getSystemService(Context.BLUETOOTH_SERVICE);
mBluetoothAdapter = btManager.getAdapter();
//mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); //也可以这样获取
if ( null != mBluetoothAdapter ) {
if ( !mBluetoothAdapter.isEnabled() ) {
mBluetoothAdapter.enable();
// Intent enabler=new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
// startActivityForResult(enabler,1); //提示用户打开蓝牙
Toast.makeText(this, "正在打开蓝牙...", Toast.LENGTH_SHORT).show();
}
}
}

3、扫描其它蓝牙设备;

开始扫描蓝牙,扫描时间为5秒;(扫描的设备可能重复) Log输出的就是扫描到的蓝牙设备;

leScanCallback = new BluetoothAdapter.LeScanCallback() {
@Override
public void onLeScan(BluetoothDevice bluetoothDevice, int i, byte[] bytes) {
Log.i("ccb", "onLeScan: " + bluetoothDevice.getName() + "--" + bluetoothDevice.getAddress());
}
};
mHandler.postDelayed(new Runnable() {
@Override
public void run() {
mBluetoothAdapter.stopLeScan(leScanCallback);
}
}, 5000);
mBluetoothAdapter.startLeScan(leScanCallback);

4、连接蓝牙

拿到扫描到的BluetoothDevice对象,使用它去调用连接的方法;其中BluetoothGatt 就是你所需要的通信管道了;

onConnectionStateChange:连接状态发生改变时;

onServicesDiscovered:发现服务时;(服务会包含UUID,还会包含其特征,特征中的内容)

device.connectGatt(AndroidBleActivity.this, false, new BluetoothGattCallback() {
@Override
public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
super.onConnectionStateChange(gatt, status, newState);
if (newState == BluetoothGatt.STATE_CONNECTED) {
Log.e("ccb", "设备连接上 开始扫描服务");
// 开始扫描服务,安卓蓝牙开发重要步骤之一
gatt.discoverServices();
}
if (newState == BluetoothGatt.STATE_DISCONNECTED) {
Log.e("ccb", "连接断开");
}
} @Override
public void onServicesDiscovered(BluetoothGatt gatt, int status) {
super.onServicesDiscovered(gatt, status);
List<BluetoothGattService> servicesList;
//获取服务列表 服务列表中有当前设备的uuid
servicesList = gatt.getServices();
} //若写入指令成功则回调BluetoothGattCallback中的onCharacteristicWrite()方法,说明将数据已经发送给下位机。
@Override
public void onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
super.onCharacteristicWrite(gatt, characteristic, status);
} //若发送的数据符合通信协议,则下位机会向上位机回复相应的数据。发送的数据通过回调onCharacteristicChanged()方法获取
@Override
public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) {
super.onCharacteristicChanged(gatt, characteristic);
// value为设备发送的数据,根据数据协议进行解析
byte[] value = characteristic.getValue();
} @Override
public void onDescriptorWrite(BluetoothGatt gatt, BluetoothGattDescriptor descriptor, int status) {
super.onDescriptorWrite(gatt, descriptor, status);
}
}); }
});

5、发送信息到BLE设备;

characteristic就是特征,它可以包含你要发送的信息;

BluetoothGattService gattService = mBluetoothGatt.getService(UUID.fromString("服务的uuid"));
BluetoothGattCharacteristic gattCharacteristic = gattService.getCharacteristic(UUID.fromString("特征的uuid"));
gattCharacteristic.setValue(new byte[]{0x1a,0x0a});
mBluetoothGatt.writeCharacteristic(gattCharacteristic); //发送数据

先写这么多吧,有时间再补;

上一篇:Android实践 -- Android蓝牙设置连接


下一篇:python 全栈开发,Day141(flask之应用上下文,SQLAlchemy)