我試圖讓我的應用程序的服務監聽藍牙連接和斷開連接嘗試,以便我可以動態檢查/支持藍牙共享網絡通信。如何接收藍牙設備工作的意圖?
首先,我有兩個三星S4(運行CyanogenMod 10.2,這是基於Android 4.3.1),我可以配對就好了。如果我將一臺設備設置爲藍牙繫繩,另一臺連接,則會創建一個新的bt-pan網絡接口,並使用DHCP分配IP。我在shell中使用iwconfig和ifconfig證實了這一點。
我在我的應用程序下面的燙髮:(還有更多,我只是指出了BT燙髮我加的)
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
這裏是我服務的onCreate,我把我的IntentFilters:(注意我已經得到了祝酒詞在這裏,但我是用日誌記錄最初的工作)
@Override
public void onCreate() {
super.onCreate();
...
this.mLocalBroadcastManager = LocalBroadcastManager.getInstance(this);
IntentFilter filter = new IntentFilter();
filter.addAction(BluetoothDevice.ACTION_FOUND);
filter.addAction(BluetoothDevice.ACTION_ACL_CONNECTED);
filter.addAction(BluetoothDevice.ACTION_ACL_DISCONNECTED);
filter.addAction(BluetoothDevice.ACTION_ACL_DISCONNECT_REQUESTED);
mLocalBroadcastManager.registerReceiver(mMessageReceiver, filter);
}
這裏是我的廣播接收器的實現:
private BroadcastReceiver mMessageReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if(action.equals(BluetoothDevice.ACTION_FOUND)) {
Toast.makeText(getApplicationContext(), "BT found", Toast.LENGTH_SHORT).show();
} else if(action.equals(BluetoothDevice.ACTION_ACL_CONNECTED)) {
Toast.makeText(getApplicationContext(), "BT Connected",
} else if(action.equals(BluetoothDevice.ACTION_ACL_DISCONNECTED)) {
Toast.makeText(getApplicationContext(), "BT Disconnected", Toast.LENGTH_SHORT).show();
} else
Toast.makeText(getApplicationContext(), "BT Disconnect requested", Toast.LENGTH_SHORT).show();
}
}
}
現在,當我關閉/打開藍牙功能時,連接/斷開與配對設備的連接,沒有任何火災發生。我從兩端都購買了設備。沒有廣播。
任何人有一個建議?我真的需要接收這些藍牙事件。請勿指向具有相同燙髮和意圖過濾器的其他帖子/網站。謝謝。
呵呵。看起來你是對的。系統意圖需要直接註冊到服務/活動。我現在正在連接藍牙並斷開連接。但是,我的onReceive實際上處理了大量不再起作用的自定義活動<->服務意圖。我將分開我的自定義和系統接收器,以便我可以一起工作。感謝指針! – garlicman