Android 蓝牙服务器和广播接收器
我的应用程序持有一个带有特定Android 蓝牙服务器和广播接收器,android,Android,我的应用程序持有一个带有特定UUID的开放式蓝牙服务器套接字,以便其他设备连接和传输文件。关于广播接收器,我有点困惑 在扩展活动的类中,我想检查蓝牙适配器的状态。但我的广播接收器从未被触发。我试着这样使用广播接收器: public class MainClass extends Activity { public void onCreate(Bundle b) { super.onCreate(b); IntentFilter filter = new IntentFilter(Bl
UUID
的开放式蓝牙服务器套接字,以便其他设备连接和传输文件。关于广播接收器
,我有点困惑
在扩展活动的类中,我想检查蓝牙适配器的状态。但我的广播接收器从未被触发。我试着这样使用广播接收器:
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;
}
}
}
};
}
我有一个关于各州的问题:
- 状态状态_ON是否只有在运行时蓝牙打开时才会触发?或者我可以在蓝牙打开的情况下启动应用程序,然后此事件将被触发?因为如果蓝牙已打开,我想启动方法
btCom.startServer()
我还读到,我需要在清单文件中注册广播接收器,如果广播接收器位于扩展活动的类中,我该如何注册?如果我在另一节课上有这个广播接收器,我会这样做
对于Instance来说,我的包名是com.workbench
,类名是BluetoothReceiver
清单将如下所示:
<receiver android:name="com.workbench.BluetoothReceiver"></receiver>
蓝牙适配器的状态更改时,将发送广播操作蓝牙适配器。操作状态更改
。只有当适配器的状态更改时,您才会看到这一点
您可以通过调用BluetoothAdapter.isEnabled()
来检查蓝牙适配器的当前状态
如果希望在应用程序未运行时获得广播意图
,则只需在清单中注册广播接收器
。按照您实现BroadcastReceiver
(作为匿名类)的方式,无法在清单中注册它