我的应用程序持有一个开放的蓝牙服务器插座,具有特定的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>