1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
|
private class ClientBluetooth extends Thread {
private final BluetoothSocket blueSocket;
private final BluetoothDevice blueDevice;
private final Uri blueUri;
public ClientBluetooth(BluetoothDevice device, Uri uri) {
BluetoothSocket tmp = null;
blueDevice = device;
blueUri = uri;
Method m = null;
UUID uuid = blueDevice.getUuids()[0].getUuid();
System.out.println("Device UUID = " + uuid);
try {
try {
tmp = blueDevice.createRfcommSocketToServiceRecord(uuid);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("Remote device Name : " + blueDevice.getName());
try {
m = blueDevice.getClass().getMethod("createInsecureRfcommSocketToServiceRecord", new Class[] {UUID.class}); //Test
//m = blueDevice.getClass().getMethod("createRfcommSocketToServiceRecord", new Class[] {UUID.class});
} catch (NoSuchMethodException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// tmp = (BluetoothSocket) m.invoke(blueDevice, 1);
tmp = (BluetoothSocket) m.invoke(blueDevice, uuid);
} catch (IOException e)
{
System.out.println(e.getMessage());
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
blueSocket = tmp;
}
public void run() {
try {
mBluetoothAdapter.cancelDiscovery();
System.out.println("Cancel discovery : succes");
} catch (Exception e) {
System.out.println("Cancel discovery exception : " + e.getMessage());
}
new Thread(new Runnable()
{
@Override
public void run()
{
//Connection
try {
blueSocket.connect();
System.out.println("blueSocket.connect() : succeful");
} catch (Exception e) {
System.out.println("Error when trying to connect blueSocket." + e.getMessage());
}
try {
BufferedInputStream bis = null;
try{
bis = new BufferedInputStream(getContentResolver().openInputStream(blueUri));
OutputStream os = blueSocket.getOutputStream();
InputStream inputStream = bis;
int bufferSize = 1024;
byte[] buffer = new byte[bufferSize];
byteBuffer
int len = 0;
System.out.println("inputStream.read(buffer)) " + inputStream.read(buffer));
while ((len = inputStream.read(buffer)) != -1)
{
System.out.println("wrinting output");
os.write(buffer, 0, len);
}
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
finally
{
if(bis != null)
try {
bis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
} catch (Exception connectException) {
try {
blueSocket.close();
} catch (IOException closeException)
{
System.out.println(closeException.getMessage());
}
return;
}
}
}).start(); |