服务发现失败android蓝牙
我正在开发一个代码,以便与来自Micromax Tab的自定义蓝牙电路进行通信。直到套接字创建它工作正常。但是,当我尝试连接失败时,说服务发现失败或主机关闭。我尝试了所有可能的提到这个主题的帖子,提到了同样的问题。但最终得到同样的错误。我试图改变UUID,但没有任何作用。任何线索? 下面是我的代码:服务发现失败android蓝牙
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();
}
}
您正在使用此人送的图片
@Override
protected void onPostExecute(Void result) {
if(selectedImageURI!=null)
{
ContentValues values = new ContentValues();
values.put(BluetoothShare.URI, selectedImageURI.toString());
Toast.makeText(getBaseContext(), "URi : " + selectedImageURI,
Toast.LENGTH_LONG).show();
values.put(BluetoothShare.DESTINATION, addressPairedDevice);
values.put(BluetoothShare.DIRECTION,
BluetoothShare.DIRECTION_OUTBOUND);
Long ts = System.currentTimeMillis();
values.put(BluetoothShare.TIMESTAMP, ts);
getContentResolver().insert(BluetoothShare.CONTENT_URI,
values);
}
BluetoothShare.java是在链路
How to send file using bluetooth on android programatically?
How to send file from Android device to other device through Bluetooth by code
谢谢Satheesh。但我想发送一个简单的字符串作为消息从平板电脑到我的自定义电路端口。你有没有这样的代码? –
看到下面的第二个链接...如果我的回答对你有用我投票 – Satheesh
我发现了这个问题。这完全是我的错。接收设备由于电池电量不足而关闭。无论如何感谢您的帮助,一定会投票给您。 –
你在问什么。你想配对另一个远程设备或发送数据到该设备... – Satheesh
发送数据到设备。我已经手动配对。并试图连接套接字发送数据。其他设备是一个电路有蓝牙调制解调器 –
等待我将发送示例代码连接并发送数据到该设备 – Satheesh