所以,最近我学习了一系列的教程http://www.youtube.com/playlist?list=PL2cK5QO_pN1gfEcWZ8tCWUb-WyxAiMyIK,用蓝牙模块HC-05连接了Arduino和安卓
我完全按照他的方案做了,蓝牙模块在我的android上被检测为HC-05,但不能配对。红色指示灯持续闪烁。正如http://mcuoneclipse.com/2013/06/19/using-the-hc-06-bluetooth-module/所说,模块上的红色LED指示状态:闪烁:准备配对稳定亮起:已配对
下面是我应该在我的设备名称旁边得到输出“(配对)”的代码
receiver = new 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);
devices.add(device);
String s = "";
for(int a=0;a<pairedDevices.size();a++){
if (device.getName().equals(pairedDevices.get(a))){
//append
s = "(Paired)";
break;
}
}
listAdapter.add(device.getName()+" "+s+" "+"\n"+device.getAddress());
}else if (BluetoothAdapter.ACTION_DISCOVERY_STARTED.equals(action)){
}else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)){
}else if (BluetoothAdapter.ACTION_STATE_CHANGED.equals(action)){
if (btAdapter.getState() == btAdapter.STATE_OFF){
turnOnBT();
}
}
}
};相反,我得到了一个祝酒词,说这个设备没有配对
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
// TODO Auto-generated method stub
if (btAdapter.isDiscovering()){
btAdapter.cancelDiscovery();
}
if (listAdapter.getItem(arg2).contains("(Paired)")){
BluetoothDevice selectedDevice = devices.get(arg2);
ConnectThread connect = new ConnectThread(selectedDevice);
connect.start();
}else {
Toast.makeText(getApplicationContext(), "device is not paired", 0).show();
}
}我错过了什么?注:我使用的是带有两个芯片的外部电源模块HC-05 (视频上只有一个芯片) Arduino UNO (视频上使用Android Pro Mini)
发布于 2013-11-05 17:09:11
我找到了我自己的答案,在使用我们自己的安卓应用程序连接之前,我们必须首先从系统settings>bluetooth>输入蓝牙模块的密码(在我的情况下是1234)
发布于 2015-07-01 17:51:50
你也可以让你的HC05通过你的代码连接,而不是从设置中配对。您只是在代码中犯了一个错误:在onItemClick方法中,您正在检查设备是否已配对,然后调用connect线程,如果未配对,则显示toast表示设备未配对,但这没有任何意义。如果它不包含"paired“,你就应该连接它,否则你需要创建一个connectThread对象并调用connect方法。希望这能起作用!如果没有,请告诉我。
https://stackoverflow.com/questions/19782103
复制相似问题