我在Android和Arduino之间找到了蓝牙的源代码,我复制并粘贴了它。
它没有显示任何代码错误。
当我试图运行它时,它会因消息而中断
"Fatal Error - In onResume() and an exception occureed during write :
socket closde. Check that the SPP
UUID:00001101-0000-1000-8000-00805f9b34fb exists on server"
问题出在哪里,怎么办?
以下是源代码:
public class MainActivity extends Activity {
private static final String TAG = "bluetooth1";
Button btnOn, btnOff;
private BluetoothAdapter btAdapter = null;
private BluetoothSocket btSocket = null;
private OutputStream outStream = null;
// SPP UUID service
private static final UUID MY_UUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
// MAC-address of Bluetooth module (you must edit this line)
private static String address = "00:15:FF:F2:19:5F";
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnOn = (Button) findViewById(R.id.btnOn);
btnOff = (Button) findViewById(R.id.btnOff);
btAdapter = BluetoothAdapter.getDefaultAdapter();
checkBTState();
btnOn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
sendData("1");
Toast.makeText(getBaseContext(), "Turn on LED", Toast.LENGTH_SHORT).show();
}
});
btnOff.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
sendData("0");
Toast.makeText(getBaseContext(), "Turn off LED", Toast.LENGTH_SHORT).show();
}
});
}
private BluetoothSocket createBluetoothSocket(BluetoothDevice device) throws IOException {
if(Build.VERSION.SDK_INT >= 10){
try {
final Method m = device.getClass().getMethod("createRFCommSocketToServiceRecord", new Class[] { UUID.class });
return (BluetoothSocket) m.invoke(device, MY_UUID);
} catch (Exception e) {
Log.e(TAG, "Could not create Insecure RFComm Connection",e);
}
}
return device.createRfcommSocketToServiceRecord(MY_UUID);
}
@Override
public void onResume() {
super.onResume();
Log.d(TAG, "...onResume - try connect...");
// Set up a pointer to the remote node using it's address.
BluetoothDevice device = btAdapter.getRemoteDevice(address);
// Two things are needed to make a connection:
// A MAC address, which we got above.
// A Service ID or UUID. In this case we are using the
// UUID for SPP.
try {
btSocket = createBluetoothSocket(device);
} catch (IOException e1) {
errorExit("Fatal Error", "In onResume() and socket create failed: " + e1.getMessage() + ".");
}
// Discovery is resource intensive. Make sure it isn't going on
// when you attempt to connect and pass your message.
btAdapter.cancelDiscovery();
// Establish the connection. This will block until it connects.
Log.d(TAG, "...Connecting...");
try {
btSocket.connect();
Log.d(TAG, "...Connection ok...");
} catch (IOException e) {
try {
btSocket.close();
} catch (IOException e2) {
errorExit("Fatal Error", "In onResume() and unable to close socket during connection failure" + e2.getMessage() + ".");
}
}
// Create a data stream so we can talk to server.
Log.d(TAG, "...Create Socket...");
try {
outStream = btSocket.getOutputStream();
} catch (IOException e) {
errorExit("Fatal Error", "In onResume() and output stream creation failed:" + e.getMessage() + ".");
}
}
@Override
public void onPause() {
super.onPause();
Log.d(TAG, "...In onPause()...");
if (outStream != null) {
try {
outStream.flush();
} catch (IOException e) {
errorExit("Fatal Error", "In onPause() and failed to flush output stream: " + e.getMessage() + ".");
}
}
try {
btSocket.close();
} catch (IOException e2) {
errorExit("Fatal Error", "In onPause() and failed to close socket." + e2.getMessage() + ".");
}
}
private void checkBTState() {
// Check for Bluetooth support and then check to make sure it is turned on
// Emulator doesn't support Bluetooth and will return null
if(btAdapter==null) {
errorExit("Fatal Error", "Bluetooth not support");
} else {
if (btAdapter.isEnabled()) {
Log.d(TAG, "...Bluetooth ON...");
} else {
//Prompt user to turn on Bluetooth
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, 1);
}
}
}
private void errorExit(String title, String message){
Toast.makeText(getBaseContext(), title + " - " + message, Toast.LENGTH_LONG).show();
finish();
}
private void sendData(String message) {
byte[] msgBuffer = message.getBytes();
Log.d(TAG, "...Send data: " + message + "...");
try {
outStream.write(msgBuffer);
} catch (IOException e) {
String msg = "In onResume() and an exception occurred during write: " + e.getMessage();
if (address.equals("00:00:00:00:00:00"))
msg = msg + ".\n\nUpdate your server address from 00:00:00:00:00:00 to the correct address on line 35 in the java code";
msg = msg + ".\n\nCheck that the SPP UUID: " + MY_UUID.toString() + " exists on server.\n\n";
errorExit("Fatal Error", msg);
}
}
}
这是一个糟糕的代码,您不应该使用它--在Google-BluetoothChatService中有一个很好的示例:https://github.com/googleSamples/android-BluetoothChat。请参见BluuetoothChatService类。不幸的是,它有一个问题--它使用处理程序与应用程序的其余部分通信。我对它做了一点修改,以便它将消息发送到另一个HandlerThread,该线程通知用户设置的自定义接口(对于我的Arduino项目),有关更多细节,请参见此项目:https://github.com/Alexshutov/ledlights
我有一个按钮,可以从耳机到扬声器再到蓝牙等等。 这是我的代码: 但当我听到耳机时,我停止蓝牙,我将isSpeakerOn设置为false,但之后,我开始在蓝牙而不是手机的听筒中听到声音。我做错了什么?
本文向大家介绍蓝牙用法和应用,包括了蓝牙用法和应用的使用技巧和注意事项,需要的朋友参考一下 蓝牙使用 蓝牙的使用大致可分为三个领域: 数据和语音的接入点-蓝牙通过无线连接便携式和固定网络设备来提供实时语音和数据传输。 电缆更换-蓝牙替代了有线网络的大量电线和电缆。即使设备不在范围内,连接也可以立即建立并保持。设备的范围通常为10m。但是,可以通过使用放大器扩大范围。 Ad hoc网络-网络设备即席
我已经在androidmanifest文件中设置了蓝牙权限。在上面的scand函数中,它应该显示已发现设备的列表,但它显示的是一个空对话框,只有标题。请告诉我如何正确使用startdiscovery和broadcastreceiver在AlertDialog中显示结果。
这是我收到的错误消息: 08-02 11:36:06.470 311 88-31188/uk.ac.york.androidtoiosble d/BluetoothAdapter:startLeScan():null 08-02 11:36:06.470 311 88-31188/uk.ac.york.androidtoiosble e/BluetoothAdapter:startLeScan:n
jd.closeBluetoothAdapter(Object object) 关闭蓝牙模块。调用该方法将断开所有已建立的连接并释放系统资源。建议在使用蓝牙流程后,与 jd.openBluetoothAdapter 成对调用。 参数 Object object 属性 类型 默认值 必填 说明 success function 否 接口调用成功的回调函数 fail function 否 接口调用失败
蓝牙模块能够使你的作品与 Makeblock 蓝牙适配器 或支持蓝牙4.0的设备建立无线连接,使你能够无线地控制你的作品。 蓝牙的连接设置参看:通过蓝牙连接设备 蓝牙的灯效及含义 闪烁:蓝牙未被连接 常亮:蓝牙已连接,工作正常 灭:断电或故障 参数 尺寸:24×24mm 推荐使用距离:10m以内 蓝牙版本:BT4.0 频带范围:2402~2480MHz 天线增益:1.5dBi 能耗等级:≤4dBm