先上demo:https://github.com/soulListener/BlueToothDemo
最近公司要适配一款读卡器,是通过蓝牙进行CPU卡和M1卡的读写操作,没搞过蓝牙,做下记录。
Android设备操作蓝牙需要以下步骤
1、在Manifest文件中添加权限
2、判断是否支持蓝牙、是否开启蓝牙
3、获取广播列表
发送蓝牙搜索广播、蓝牙扫描结束广播(在OnDestory中反注册广播防止泄露),在搜索广播回调中获取蓝牙设备,在蓝牙扫描结束广播中进行相应的结束操作
4、连接蓝牙(连接蓝牙之前要关闭蓝牙扫描操作,区分经典蓝牙和低功耗蓝牙)
5、数据通讯
1.在manifest文件中添加权限
<uses-permission android:name="android.permission.BLUETOOTH"/>
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
如果是6.0以上的则需再加上
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
2.判断是否支持蓝牙、是否开启蓝牙
if (mBluetoothAdapter == null) {
Toast.makeText(this, "设备不支持蓝牙", Toast.LENGTH_SHORT).show();
finish();
}
// 判断是否打开蓝牙
if (!mBluetoothAdapter.isEnabled()) {
// 弹出对话框提示用户是后打开
Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
Toast.makeText(this, "正在搜索", Toast.LENGTH_SHORT).show();
startActivityForResult(intent, SEARCH_CODE);
} else {
// 打开蓝牙
Toast.makeText(this,"正在打开连接",Toast.LENGTH_SHORT).show();
mBluetoothAdapter.enable();
}
3.有两种实现形式
(1)发送蓝牙搜索广播、蓝牙扫描结束广播,在搜索广播回调中获取蓝牙设备,在蓝牙扫描结束广播中进行相应的结束操作
if (mBluetoothAdapter.isDiscovering()) {
mBluetoothAdapter.cancelDiscovery();
}
//开始扫描
mBluetoothAdapter.startDiscovery();
receiver = new BluetoothReceiver();
//蓝牙扫描广播
IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
registerReceiver(receiver, filter);
//蓝牙扫描结束广播
IntentFilter filter1 = new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
registerReceiver(receiver, filter1);
还有广播
private class BluetoothReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (BluetoothDevice.ACTION_FOUND.equals(action)) {
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
mBluelist.add(device);
myBlueToothAdapter.notifyDataSetChanged();
} else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)){
mBluetoothAdapter.cancelDiscovery();
}
}
}
(2)SDK21版本以上的可以使用mBluetoothAdapter.getBluetoothLeScanner().startScan(ScanCallback)来获取列表
mBluetoothAdapter.getBluetoothLeScanner().startScan(new ScanCallback() {
@Override
public void onScanResult(int callbackType, ScanResult result) {
mBluelist.add(result.getDevice());
myBlueToothAdapter.notifyDataSetChanged();
}
});
4.连接蓝牙
在这里要特别区分一下,蓝牙连接分为经典蓝牙 和 低功耗蓝牙,两种蓝牙的连接方式是不一样的。我在这里卡了半天调不通才发现蓝牙还有这种区别(都怪自己技术差)。
(1)经典蓝牙
经典蓝牙通讯通过BlueToothSocket这个类,读写数据跟java中的Socket是一样的。获取方式是通过
BlueToothSocket blueToothSocket = device.createRfcommSocketToServiceRecord(UUID);
blueToothSocket.connect();
(2)低功耗蓝牙
低功耗蓝牙通过Device的connectGatt方法进行连接,在BluetoothGattCallback回调中进行控制。
BluetoothGatt bluetoothGatt = device.connectGatt(this, false,bluetoothGattCallback);
5.数据通讯
(1)经典蓝牙
读写数据通过IO流就可以:
InputStream inputStream = blueToothSocket .getInputStream();
OutputStream outputStream = blueToothSocket .getOutputStream();
(2)低功耗蓝牙
通过BluetoothGattCharacteristic获取对应的UUID进行读写数据
BluetoothGattService bgs = gatt.getService(UUID.fromString("ServiceUUID"));
BluetoothGattCharacteristic mBleWriter = bgs.getCharacteristic(UUID.fromString("BlueWriterUUID"));
BluetoothGattCharacteristic mBleReader = bgs.getCharacteristic(UUID.fromString("BlueReaderUUID"));
Log.d("mBleWriter", "mBleWriter: type " + mBleWriter.getWriteType() + ", " + mBleWriter.getDescriptors().size());
Log.d("mBleReader", "mBleReader: type " + mBleReader.getWriteType() + ", " + mBleReader.getDescriptors().size());
mBleWriter.setValue("1111".getBytes());
bluetoothGatt.writeCharacteristic(mBleWriter);
哦,对了还有属性变量
private ArrayList<BluetoothDevice> mBluelist;
private BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
private int SEARCH_CODE = 0;
private BluetoothReceiver receiver;
private MyBlueToothAdapter myBlueToothAdapter;
private BluetoothGatt bluetoothGatt;
网友评论