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
| public class UniDS6BluetoothActivity extends Activity {
private static final UUID MY_UUID = UUID
.fromString("00001101-0000-1000-8000-09180F7294D1");
private static String address = "0A:06:66:07:6D:E6"; // address du module
Button buttonConnection;
BluetoothAdapter bluetoothAdapter;
private BluetoothSocket btSocket;
private OutputStream outStream;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Log.i("", "onCreate");
bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if (bluetoothAdapter == null) {
Toast.makeText(UniDS6BluetoothActivity.this,
"No Bluetooth devices", Toast.LENGTH_LONG).show();
Log.e("ERROR", "No Bluetooth devices");
} else {
Toast.makeText(UniDS6BluetoothActivity.this, "Bluetooth available",
Toast.LENGTH_SHORT).show();
Log.i("INFO", "Bluetooth available");
}
if (!bluetoothAdapter.isEnabled()) { // si pas activé, alors
// l'activer...
bluetoothAdapter.enable();
Log.i("INFO", "Bluetooth enable");
}
String mac = BluetoothAdapter.getDefaultAdapter().getAddress();
Log.i("MAC:", "++++" + mac + "++++"); // afficher l'adresse mac du
// nexus s
}
@Override
protected void onStart() {
Log.i("", "onStart");
super.onStart();
}
@Override
protected void onResume() {
Log.i("", "onResume");
BluetoothDevice device = bluetoothAdapter.getRemoteDevice(address);
try {
btSocket = device.createRfcommSocketToServiceRecord(MY_UUID);
Log.i("INFO", "Socket OK");
} catch (IOException e) {
Log.e("ERROR", "ON RESUME: Socket creation failed.", e);
}
bluetoothAdapter.cancelDiscovery();
try {
btSocket.connect();
Log.i("INFO","ON RESUME: BT connection established, data transfer link open.");
} catch (IOException e) {
try {
btSocket.close();
} catch (IOException e2) {
Log.e("ERROR","ON RESUME: Unable to close socket during connection failure",e2);
}
}
try {
outStream = btSocket.getOutputStream();
Log.i("INFO", "btSocket.getOutputStream(); OK");
} catch (IOException e) {
Log.e("ERROR", "ON RESUME: Output stream creation failed.", e);
}
if (outStream != null) {
try {
outStream.close();
} catch (Exception e) {
}
outStream = null;
}
if (btSocket != null) {
try {
btSocket.close();
} catch (Exception e) {
}
btSocket = null;
}
try {
btSocket.connect();
} catch (IOException e1) {
Log.e("ERROR", "btSocket.connect();", e1);
}
String message = "NEXUS S";
byte[] msgBuffer = message.getBytes();
try {
outStream.write(msgBuffer);
} catch (IOException e) {
Log.e("ERROR", "ON RESUME: Exception during write.", e);
}
super.onResume();
}
@Override
protected void onPause() {
Log.i("", "onPause");
if (outStream != null) {
try {
outStream.flush();
} catch (IOException e) {
Log.e("ERROR", "ON PAUSE: Couldn't flush output stream.", e);
}
}
try {
btSocket.close();
} catch (IOException e2) {
Log.e("ERROR", "ON PAUSE: Unable to close socket.", e2);
}
super.onPause();
}
@Override
protected void onStop() {
Log.i("", "onStop");
super.onStop();
}
@Override
protected void onDestroy() {
Log.i("", "onDestroy");
if (bluetoothAdapter.isEnabled()) {
bluetoothAdapter.cancelDiscovery();
bluetoothAdapter.disable();
Log.i("INFO", "Bluetooth disable... Application terminated");
}
super.onDestroy();
}
} |
Partager