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 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206
|
package com.meetphone;
import java.io.IOException;
import java.util.Set;
import java.util.UUID;
import android.app.Service;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothServerSocket;
import android.bluetooth.BluetoothSocket;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.net.ConnectivityManager;
import android.os.AsyncTask;
import android.os.IBinder;
import android.preference.PreferenceManager;
import android.util.Log;
import android.widget.Toast;
public class BluetoothService extends Service {
private static final boolean DEBUG = false;
private static final String TAG = "BluetoothService";
private BluetoothAdapter bluetoothAdapter = null;
public String dStarted = BluetoothAdapter.ACTION_DISCOVERY_STARTED;
public String dFinished = BluetoothAdapter.ACTION_DISCOVERY_FINISHED;
private static final int REQUEST_ENABLE_BT = 1;
private static final int DISCOVERY_REQUEST = 1;
private static final UUID uuid = UUID.fromString("a60f35f0-b93a-11de-8a39-08002009c666");
SharedPreferences preferences = null;
private String service;
@SuppressWarnings("unused")
private ConnectivityManager connectivity;
@Override
public void onCreate() {
super.onCreate();
if(DEBUG) Log.e(TAG, "onCreate");
Context context = getApplicationContext();
preferences = PreferenceManager.getDefaultSharedPreferences(context);
bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if(bluetoothAdapter != null) {
if(preferences.getBoolean("BLUETOOTH_AUTO", true) && !bluetoothAdapter.isEnabled()){
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivity(enableBtIntent);
//Intent intent2 = new Intent (BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
//intent2.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 300);
//startActivity(intent2);
//startActivityForResult(enableBtIntent, DISCOVERY_REQUEST);
}
}
else{
Toast.makeText(this, "Votre appareil ne supporte pas le bluetooth", Toast.LENGTH_LONG);
//finish();
}
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
super.onStart(intent, startId);
if(DEBUG) Log.e(TAG, "onStartCommand");
BroadcastReceiver discoveryMonitor = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent){
if(dStarted.equals(intent.getAction())) {
//discover start
Toast.makeText(getApplicationContext(), "Decouverte en cours...", Toast.LENGTH_SHORT).show();
}
else if(dFinished.equals(intent.getAction())) {
//discover finish
Toast.makeText(getApplicationContext(), "Decouverte terminée", Toast.LENGTH_SHORT).show();
bluetoothAdapter.cancelDiscovery();
}
}
};
registerReceiver(discoveryMonitor, new IntentFilter(dStarted));
registerReceiver(discoveryMonitor, new IntentFilter(dFinished));
final BluetoothDevice device = bluetoothAdapter.getRemoteDevice("01:23:77:35:2F:AA");
final Set<BluetoothDevice> bondedDevices = bluetoothAdapter.getBondedDevices();
BroadcastReceiver discoveryResult = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String remoteDeviceName = intent.getStringExtra(BluetoothDevice.EXTRA_NAME);
BluetoothDevice remoteDevice;
remoteDevice = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
Toast.makeText(getApplicationContext(), "Decouvert : " + remoteDeviceName, Toast.LENGTH_LONG).show();
//if(remoteDevice.equals(device) && bondedDevices.contains(remoteDevice)){
try{
BluetoothDevice device = bluetoothAdapter.getRemoteDevice("00:23:76:35:2F:AA");
BluetoothSocket clientSocket = device.createRfcommSocketToServiceRecord(uuid);
clientSocket.connect();
Toast.makeText(getApplicationContext(), "connecté", Toast.LENGTH_LONG).show();
//TODO :transferer les donnée utiliser fct sendData
}catch(IOException e){
Log.d("Bluetooth",e.getMessage());
}
//}
}
};
registerReceiver(discoveryResult, new IntentFilter(BluetoothDevice.ACTION_FOUND));
BroadcastReceiver bluetoothState = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String prevStateExtra = BluetoothAdapter.EXTRA_PREVIOUS_STATE;
String stateExtra = BluetoothAdapter.EXTRA_STATE;
int state = intent.getIntExtra(stateExtra, -1);
String tt="";
switch (state) {
case (BluetoothAdapter.STATE_TURNING_ON) : {
tt="Bluetooth en cours d'activation";
break;
}
case (BluetoothAdapter.STATE_ON) : {
tt="Bluetooth activé";
unregisterReceiver(this);
break;
}
case (BluetoothAdapter.STATE_TURNING_OFF) : {
tt = "Bluetooth en cours de désactivation";
break;
}
case (BluetoothAdapter.STATE_OFF) : {
tt = "Bluetooth désactivé";
break;
}
default: break;
}
Toast.makeText(BluetoothService.this, tt, Toast.LENGTH_SHORT).show();
}
};
String actionStateChanged = BluetoothAdapter.ACTION_STATE_CHANGED;
String actionRequestEnable = BluetoothAdapter.ACTION_REQUEST_ENABLE;
registerReceiver(bluetoothState, new IntentFilter(actionStateChanged));
if(!bluetoothAdapter.isDiscovering()){
dStarted = BluetoothAdapter.ACTION_DISCOVERY_STARTED;
dFinished = BluetoothAdapter.ACTION_DISCOVERY_FINISHED;
bluetoothAdapter.startDiscovery();
}
return START_STICKY;
}
@Override
public IBinder onBind(Intent intent) {
if(DEBUG) Log.e(TAG, "onBind");
return null;
}
//@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) throws IOException {
if (requestCode == DISCOVERY_REQUEST) {
boolean isDiscoverable = resultCode > 0;
if (isDiscoverable) {
String name = "bluetoothserver";
final BluetoothServerSocket btserver = bluetoothAdapter.listenUsingRfcommWithServiceRecord(name, uuid);
Thread acceptThread = new Thread(new Runnable() {
public void run(){
try{
BluetoothSocket serverSocket = btserver.accept();
} catch (IOException e) {
Log.d("BLUETOOTH", e.getMessage());
}
}
});
acceptThread.start();
}
}
}
} |
Partager