我正在开发一个代码,用于与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();
}
}
答案 0 :(得分:0)
您正在使用此图片发送图片...
@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