我的應用程序持有一個開放的藍牙服務器插座,具有特定的UUID
,以便其他設備連接和傳輸文件。我對BroadcastReceiver
有點困惑。藍牙服務器和BroadCastReceiver
在我的課堂延伸Activity
,我要檢查藍牙適配器的狀態。但我的BroadcastReceiver從未被觸發。我嘗試使用廣播接收器是這樣的:
public class MainClass extends Activity {
public void onCreate(Bundle b) {
super.onCreate(b);
IntentFilter filter = new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED);
registerReceiver(mReceiver, filter);
}
private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
final String action = intent.getAction();
Log.w("BroadcastReceiver: ", "Inside!");
if (action.equals(BluetoothAdapter.ACTION_STATE_CHANGED)) {
final int state = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, BluetoothAdapter.ERROR);
switch (state) {
case BluetoothAdapter.STATE_OFF:
Log.d("Bluetooth Receiver", "State-off");
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
break;
case BluetoothAdapter.STATE_TURNING_OFF:
Log.d("Bluetooth Receiver", "State turning off");
break;
case BluetoothAdapter.STATE_ON:
Log.d("Bluetooth Receiver", "State-on");
btCom = new BluetoothCommunicator(MainClass.this, lastCases, nist);
btCom.startServer();
break;
case BluetoothAdapter.STATE_TURNING_ON:
Log.d("Bluetooth Receiver", "State turning on");
break;
}
}
}
};
}
我有關於狀態的問題:
- 狀態STATE_ON是在運行期間,啓用藍牙功能這只是發射了?或者我可以啓動藍牙開啓我的應用程序,這個事件將被解僱?因爲我要開始的方法
btCom.startServer()
如果啓用藍牙功能
我也看了,我需要在我的清單文件中註冊廣播接收器,我怎麼可以這樣做,如果廣播接收器是在一個延伸活動類?如果我在一個單獨的類有這個廣播接收器我會做這樣的
地說instace我的包名稱爲com.workbench
和我的類名BluetoothReceiver
該清單將是這個樣子:
<receiver android:name="com.workbench.BluetoothReceiver"></receiver>