我正在开发一个代码来从 Micromax Tab 与自定义蓝牙电路进行通信。直到套接字创建它工作正常。但是当我尝试连接时,它会说服务发现失败或主机已关闭。我尝试了所有可能提到这个主题的帖子,都提到了同样的问题。但最终得到同样的错误。我试图更改 UUID 但没有任何效果。有什么线索吗?下面是我的代码:
final BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if (mBluetoothAdapter == null) {
Toast.makeText(this, "Bluetooth not found",Toast.LENGTH_LONG).show();
}
if (!mBluetoothAdapter.isEnabled()) {
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
}
Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices();
// If there are paired devices
if (pairedDevices.size() > 0) {
// Loop through paired devices
for (BluetoothDevice device : pairedDevices) {
// Add the name and address to an array adapter to show in a ListView
if(device.getName().equals("linvor"))
{
mmDevice = device;
Toast.makeText(this, "Name: " + device.getName() + " And Address: " + device.getAddress(),Toast.LENGTH_LONG).show();
}
}
}
mBluetoothAdapter.cancelDiscovery();
BluetoothSocket tmp = null;
// UUID uuid = UUID.fromString("00001105-0000-1000-8000-00805F9B34FB");
// MY_UUID is the app's UUID string, also used by the server code
// tmp = mmDevice.createRfcommSocketToServiceRecord(uuid);
Method m = null;
try {
m = mmDevice.getClass().getMethod("createRfcommSocket", new Class[] {int.class});
try {
tmp = (BluetoothSocket) m.invoke(mmDevice, 1);
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
Toast.makeText(getBaseContext(), e.getMessage(),Toast.LENGTH_LONG).show();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
Toast.makeText(getBaseContext(), e.getMessage(),Toast.LENGTH_LONG).show();
} catch (InvocationTargetException e) {
// TODO Auto-generated catch block
Toast.makeText(getBaseContext(), e.getMessage(),Toast.LENGTH_LONG).show();
}
} catch (NoSuchMethodException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
tmp = (BluetoothSocket) m.invoke(mmDevice, 1);
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
Toast.makeText(getBaseContext(), e.getMessage(),Toast.LENGTH_LONG).show();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
Toast.makeText(getBaseContext(), e.getMessage(),Toast.LENGTH_LONG).show();
} catch (InvocationTargetException e) {
// TODO Auto-generated catch block
Toast.makeText(getBaseContext(), e.getMessage(),Toast.LENGTH_LONG).show();
}
Toast.makeText(getBaseContext(), "Created socket",Toast.LENGTH_LONG).show();
mmSocket = tmp;
mBluetoothAdapter.cancelDiscovery();
Toast.makeText(getBaseContext(), "Discovery cancelled",Toast.LENGTH_LONG).show();
try {
// Connect the device through the socket. This will block
// until it succeeds or throws an exception
mmSocket.connect();
Toast.makeText(getBaseContext(), "Connected",Toast.LENGTH_LONG).show();
} catch (IOException e1) {
// Unable to connect; close the socket and get out
Toast.makeText(getBaseContext(), e1.getMessage(),Toast.LENGTH_LONG).show();
try {
mmSocket.close();
} catch (IOException e) {
Toast.makeText(getBaseContext(), e.getMessage(),Toast.LENGTH_LONG).show();
}
}