Java 启用蓝牙会中断广播接收器
我的应用程序旨在将所有扫描的蓝牙设备附加到Java 启用蓝牙会中断广播接收器,java,android,bluetooth,Java,Android,Bluetooth,我的应用程序旨在将所有扫描的蓝牙设备附加到文本视图中。如果手机的蓝牙功能开启,这项功能将非常有效。但是,如果我的应用程序检查手机蓝牙是否关闭,并在蓝牙关闭时将其打开,则启动我的发现过程。我的广播接收器不会拾取事件,因此我的文本视图不会填充。感谢您的帮助 这是我的密码: @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setConte
文本视图中。如果手机的蓝牙功能开启,这项功能将非常有效。但是,如果我的应用程序检查手机蓝牙是否关闭,并在蓝牙关闭时将其打开,则启动我的发现过程。我的广播接收器
不会拾取事件,因此我的文本视图
不会填充。感谢您的帮助
这是我的密码:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_blue_tooth_main);
txtResults = (TextView) this.findViewById(R.id.txtResults);
mBlueToothAdapter = BluetoothAdapter.getDefaultAdapter();
if (!(mBlueToothAdapter.isEnabled())) {
mBlueToothAdapter.enable();
}
mBlueToothAdapter.startDiscovery();
我的接收人:
public static class BlueToothBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
BluetoothDevice device = intent
.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
blueToothName = device.getName();
blueToothAddress = device.getAddress();
blueToothClass = device.getBluetoothClass();
blueToothBondState = device.getBondState();
GetBondStateStr(blueToothBondState);
blueToothUUIDS = device.getUuids();
paramsBlueTooth
.add(new BasicNameValuePair("Name: ", blueToothName));
paramsBlueTooth.add(new BasicNameValuePair("Address: ",
blueToothAddress));
paramsBlueTooth.add(new BasicNameValuePair("Class: ", String
.valueOf(blueToothClass)));
paramsBlueTooth.add(new BasicNameValuePair("Bond State: ",
blueToothBondStateStr));
paramsBlueTooth.add(new BasicNameValuePair("UUIDS: ", String
.valueOf(blueToothUUIDS)));
showBlueToothData();
}
ShowBlueToothData():
试着做:
Intent enable_intent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enable_intent, REQUEST_ENABLE_BT);
内部if(!(mBlueToothAdapter.isEnabled()){…}
或:
for(int i=0;i您选择的通过BluetoothAdapter.enable()启用蓝牙收音机的方法
方法是一个异步调用。从中可以看出,您不能假设收音机在方法返回后立即启动并处于活动状态。您必须等待操作\u状态\u更改
广播,才能知道收音机已准备好供您尝试扫描
如果您阅读了文档,请注意,这样做是一种糟糕的体验,因为没有通知用户。更好的选择是将用户发送到设置以启用蓝牙。您可以将启动逻辑修改为类似以下内容:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_blue_tooth_main);
txtResults = (TextView) this.findViewById(R.id.txtResults);
mBlueToothAdapter = BluetoothAdapter.getDefaultAdapter();
…
}
@Override
protected void onResume() {
super.onResume();
if (!(mBlueToothAdapter.isEnabled())) {
//Take the user to settings first!
Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivity(intent);
} else {
mBlueToothAdapter.startDiscovery();
}
}
此修改将在您每次来到前台时检查蓝牙,并且仅在您准备好进行扫描时才触发扫描。您可以发布相关代码吗?第一个错误“请求启用无法解析为变量”@John在你的活动顶部写了这样一行:private final static int REQUEST_ENABLE_BT=1;因此,似乎你的代码和我的代码都可以工作,但只有在我旋转手机时才能工作。这可能是因为没有暂停和恢复当前活动吗?@John当手机旋转时,Android操作系统会重新启动当前活动。
for(int i=0;i<5;i++){
if (!mBluetoothAdapter.isEnabled()) {
mBluetoothAdapter.enable();
}
Thread.sleep(100);
}else{
//display error, unsuccessfull , try manually
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_blue_tooth_main);
txtResults = (TextView) this.findViewById(R.id.txtResults);
mBlueToothAdapter = BluetoothAdapter.getDefaultAdapter();
…
}
@Override
protected void onResume() {
super.onResume();
if (!(mBlueToothAdapter.isEnabled())) {
//Take the user to settings first!
Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivity(intent);
} else {
mBlueToothAdapter.startDiscovery();
}
}