在Android平臺開發(fā)藍牙應(yīng)用程序
通過自己的學(xué)習(xí)和探索,講解一下Android平臺下,上層藍牙應(yīng)用的開發(fā)。
開發(fā)Android藍牙應(yīng)用的基本步驟
- 以下代碼中的變量
BluetoothAdapter bluetoothAdapter;
- 設(shè)置權(quán)限
自從Android6.0以后,在一些比較危險的權(quán)限上要求必須申請動態(tài)權(quán)限。但是申請藍牙權(quán)限還是直接申請靜態(tài)權(quán)限即可。(在AndroidManifest.xml中聲明使用藍牙權(quán)限)
<uses-permission android:name=“android.permission.BLUETOOTH”/>
<uses-permission android:name=“android.permission.BLUETOOTH_ADMIN”/>
- 啟動藍牙
在啟動藍牙之前,還需要做一個操作,那就是需要判斷本機是否支持藍牙。(童鞋們,虛擬機是沒有藍牙的哦)
//啟動藍牙
void startBluetooth(){
bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if (bluetoothAdapter == null)
//說明不支持藍牙
return;
if (!bluetoothAdapter.isEnabled()){ //檢測藍牙是否開啟
//沒有開啟藍牙,則開啟藍牙
Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(intent,1);
}else {
//藍牙正常開啟
}
}
順便說一下startActivityForResult(Intent intent, int requestCode)。此方法可以判斷是否調(diào)用成功,還可以判斷從哪個activity返回來的。 比如ActivityB和ActivityC都可以start ActivityA。因為傳入的requestCode不同,所以ActivityA可以判斷是哪個Activity調(diào)用的,從而進行不同的操作。 需要重寫@Override onActivityResult方法
- 發(fā)現(xiàn)藍牙
- 使藍牙處于可見(即處于易被搜索到的狀態(tài)),便于其他設(shè)備發(fā)現(xiàn)本機藍牙
//使本機藍牙在300s內(nèi)可被搜索
private void ensureDiscoverable(){
if (bluetoothAdapter.getScanMode() != BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE){
Intent discoverIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
discoverIntent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION,300);
startActivity(discoverIntent);
}
}
- 查找已經(jīng)配對過的藍牙設(shè)備,即以前已經(jīng)配對過的設(shè)備。
public Set getBluetoothDevices(){
Set<BluetoothDevice> bluetoothDevices = bluetoothAdapter.getBondedDevices();
if (bluetoothDevices.size() > 0){
for (BluetoothDevice bluetoothDevice : bluetoothDevices){
Log.d(“Bluetooth”,bluetoothDevice.getName() + “ | “ + bluetoothDevice.getAddress());
}
}else {
//沒有設(shè)備
}
return bluetoothDevices;
}
- 接下來比較重要,藍牙如何搜索設(shè)備。再次需要注冊一個BroadcastReceiver來獲取這個搜索結(jié)果。即先注冊信息,然后進行處理。再次建議都用動態(tài)注冊的方式,因為Android8.0已經(jīng)不支持靜態(tài)注冊。
//藍牙廣播
private BroadcastReceiver bluetoothBroadcastReceive = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (BluetoothDevice.ACTION_FOUND.equals(action)){
//發(fā)現(xiàn)設(shè)備,可以看見本機藍牙可以搜索到哪些設(shè)備
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
//判斷搜索到的藍牙是否已經(jīng)配對
if (device.getBondState() == BluetoothDevice.BOND_BONDED){
//已經(jīng)配對成功的
}else{
//尚未配對成功的
}
}else if (BluetoothAdapter.ACTION_DISCOVERY_STARTED.equals(action)){
//正在搜索
}else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)){
//搜索結(jié)束
}
}
};
//動態(tài)注冊藍牙廣播
IntentFilter bluetoothIntent = new IntentFilter();
bluetoothIntent.addAction(BluetoothDevice.ACTION_FOUND); bluetoothIntent.addAction(BluetoothAdapter.ACTION_DISCOVERY_STARTED);
bluetoothIntent.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
registerReceiver(bluetoothBroadcastReceive,bluetoothIntent);
最后別忘了注銷廣播,使用方法:
unregisterReceiver(bluetoothBroadcastReceive,bluetoothIntent)