在开发蓝牙功能的时候在网上下了一个demo,demo代码移植在我的一个测试项目上是完全可以查找和连接的,但是吧代码移植到我的正式工程的时候总是不行不知道怎么回事(报错信息在网上也不好找到解决原因),这里有这方面熟悉的朋友帮忙看下,如果如推荐的demo可以参考更为感谢
下面上代码和报错信息
查找蓝牙方法
public void searchBlueToothDevice() {
//Log.i(TAG, "searchBlueToothDevice(MainActivity.java:112)--->> " + "searchBlueToothDevice");
pdSearch = ProgressDialog.show(StockOutScan.this, "", "连接中", true, true);
pdSearch.setCanceledOnTouchOutside(false);
pdSearch.show();
mBluetoothList = new ArrayList<BluetoothBean>();
// 检查设备是否支持蓝牙
Bluetoothadapter = BluetoothAdapter.getDefaultAdapter();
if (Bluetoothadapter == null) {
Toast.makeText(this, "当前设备不支持蓝牙", Toast.LENGTH_SHORT).show();
return;
}
// 如果蓝牙已经关闭就打开蓝牙
if (!Bluetoothadapter.isEnabled()) {
Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
intent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 300);
startActivity(intent);
return;
}
// // 获取已配对的蓝牙设备
// Set<BluetoothDevice> devices = adapter.getBondedDevices();
// // 遍历
// int count = 0;
// for (BluetoothDevice pairedDevice : devices) {
// Log.i(TAG, "searchBlueToothDevice(MainActivity.java:137)--->> " + pairedDevice.getName());
// if (pairedDevice.getName() == null) {
// return;
// } else if (pairedDevice.getName().startsWith("Printer_29D0")) {
// count++;
// deviceAddress = pairedDevice.getAddress();
// mBluetoothDevice = adapter.getRemoteDevice(deviceAddress);
// connect(deviceAddress, mBluetoothDevice);
// break;
// }
// }
if (Bluetoothadapter.isEnabled()) {
//开始搜索
Bluetoothadapter.startDiscovery();
// 设置广播信息过滤
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(BluetoothDevice.ACTION_FOUND);
intentFilter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
// 注册广播接收器,接收并处理搜索结果
receiver = new MyBroadcastReceiver();
registerReceiver(receiver, intentFilter);
}
}
广播代码
public class MyBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
//找到设备,有可能重复搜索同一设备,可在结束后做去重操作
if (BluetoothDevice.ACTION_FOUND.equals(action)) {
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
if (device == null) {
return;
}
if (device.getName() == null) {
return;
}
BluetoothBean bluetoothBean = new BluetoothBean();
bluetoothBean.mBluetoothName = device.getName();
bluetoothBean.mBluetoothAddress = device.getAddress();
bluetoothBean.mBluetoothDevice = Bluetoothadapter.getRemoteDevice(bluetoothBean.mBluetoothAddress);
mBluetoothList.add(bluetoothBean);
//Log.i(TAG, "onReceive(MainActivity.java:184)--->> " + device.getName());
//Log.i(TAG, "onReceive(MainActivity.java:185)--->> " + mBluetoothList.size());
// if (device.getName().startsWith("Printer_29D0")) {
// //取消搜索
// adapter.cancelDiscovery();
// deviceAddress = device.getAddress();
// mBluetoothDevice = adapter.getRemoteDevice(deviceAddress);
// connectState = device.getBondState();
// switch (connectState) {
// // 未配对
// case BluetoothDevice.BOND_NONE:
// // 配对
// try {
// Method createBondMethod = mBluetoothDevice.getClass().getMethod("createBond");
// createBondMethod.invoke(mBluetoothDevice);
// } catch (Exception e) {
// e.printStackTrace();
// }
// break;
// // 已配对
// case BluetoothDevice.BOND_BONDED:
// if (device.getName().startsWith("Printer_29D0")) {
// connect(deviceAddress, mBluetoothDevice);
// }
// break;
// }
// }
} else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {
//Log.i(TAG, "onReceive(MainActivity.java:213)--->> " + "搜索完成");
pdSearch.dismiss();
if (0 == mBluetoothList.size())
Toast.makeText(StockOutScan.this, "搜索不到蓝牙设备", Toast.LENGTH_SHORT).show();
else {
//去重HashSet add会返回一个boolean值,插入的值已经存在就会返回false 所以true就是不重复的
HashSet<BluetoothBean> set = new HashSet<BluetoothBean>();
mBluetoothList2 = new ArrayList<BluetoothBean>();
for (BluetoothBean bean : mBluetoothList) {
boolean add = set.add(bean);
if (add) {
mBluetoothList2.add(bean);
}
}
showBluetoothPop(mBluetoothList2);
}
unregisterReceiver(receiver);
}
}
}
弹框显示搜索到的蓝牙
//弹框显示蓝牙设备
private void showBluetoothPop(final List<BluetoothBean> bluetoothList) {
bluetoothtitle = (TextView) dialogView.findViewById(R.id.bluetooth_btitle);
bluetoothlistview = (ListView) findViewById(R.id.bluetooth_listview);
if (myBluetoothAdapter == null) {
myBluetoothAdapter = new MyBluetoothAdapter(
StockOutScan.this,bluetoothList);
}
bluetoothlistview.setAdapter(myBluetoothAdapter);
// 弹出对话框
bluetoothtitle.setText("选择连接蓝牙");
// 根据listview高度刷新弹框高度
refreshHeight(bluetoothlistview);
}
报错信息在注册广播方法哪一步