, , , , Bluetooth . , , Activity ( onPause() onResume()) . , , , Bluetooth. Activity, , Bluetooth.
, , Service Bluetooth.
public class BluetoothService extends Service {
public static final String BLUETOOTH_SERIAL_UUID = "00001101-0000-1000-8000-00805F9B34FB";
private BluetoothSocket mSocket;
private String mAddress = "bluetooth_mac_address_here";
public void onCreate() {
BluetoothAdapter btAdapter = BluetoothAdapter.getDefaultAdapter();
if(btAdapter.isEnabled()) {
BluetoothDevice btDevice = btAdapter.getRemoteDevice(mAddress);
mSocket = btDevice.createRfcommSocketToServiceRecord(BLUETOOTH_SERIAL_UUID);
btAdapter.cancelDiscovery();
mSocket.connect();
}
}
}
mSocket . bluetooth mSocket.getInputStream() mSocket.getOutputStream() / . , , , Activity . .
BluetoothService onStartCommand():
public class BluetoothService extends Service {
...
public static final String ACTION_SEND_DATA = "send_data";
public static final String ACTION_RECEIVED_DATA = "received_data";
public static final String EXTRA_BLUETOOTH_DATA = "bluetooth_data";
public int onStartCommand(Intent intent, int flags, int startId) {
LocalBroadcastManager.getInstance(this).registerReceiver(new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
if(intent.getAction().equals(ACTION_SEND_DATA)) {
byte[] data = intent.getByteArrayExtra(EXTRA_BLUETOOTH_DATA);
try {
mSocket.getOutputStream().write(data);
} catch(IOException ioe) {
ioe.printStackTrace();
}
}
}
return Service.START_STICKY;
}
}
Activity , . . , LocalBroadcastReceiver . , BroadcastReceiver, , , . , , , (, ), . , :
public class MyActivity extends Activity {
public void onCreate(Bundle savedInstanceState) {
...
String myString = "This is some data I want to send!";
Intent sendIntent = new Intent(BluetoothService.ACTION_SEND_DATA);
sendIntent.putExtra(BluetoothService.EXTRA_BLUETOOTH_DATA, myString.getBytes());
LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
}
}
, , , . , , . TransferManager , InputStream BluetoothSocket.
=============================================== ===========================
, , Bluetooth. , , , . , , Activities, . , , . , , , , , Bluetooth ( ), , Service. , Thread BluetoothService:
public class BluetoothService extends Service {
...
public void onCreate() {...}
public int onStartCommand(...) {...}
public static class ReceiveThread extends Thread {
private boolean isRunning;
private InputStream mBluetoothInputStream;
public ReceiveThread(InputStream bluetoothInputStream) {
mBluetoothInputStream = bluetoothInputStream;
isRunning = true;
}
@Override
public void run() {
BufferedReader bufferedReader = new BufferedReader(
new InputStreamReader(mBluetoothInputStream));
String line;
while(isRunning) {
try {
line = bufferedReader.readLine();
} catch(IOException ioe) {
ioe.printStackTrace();
isRunning = false;
}
if(line == null || line.equals("") {
continue;
}
Intent receivedIntent = new Intent(BluetoothService.this, MyActivity.class);
receivedIntent.setAction(ACTION_RECEIVED_DATA);
receivedIntent.putExtra(EXTRA_BLUETOOTH_DATA);
LocalBroadcastManager.getInstance(BluetoothService.this).sendBroadcast(receivedIntent);
try {
Thread.sleep(20);
} catch(InterruptedException e) {
e.printStackTrace();
isRunning = false;
}
}
}
}
}
, ReceiveThread, onStartCommand() :
ReceiveThread receiver = new ReceiveThread(mSocket.getInputStream());
receiver.start();
- . BroadcastReceiver, , . Activity onCreate():
public void onCreate() {
...
LocalBroadcastManager.getInstance(this).registerReceiver(new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
byte[] data = intent.getByteArrayExtra(BluetoothService.EXTRA_BLUETOOTH_DATA);
}
}, new IntentFilter(BluetoothService.ACTION_RECEIVED_DATA));
}
onReceive() , BluetoothService ReceiveThread Bluetooth. (, / ). , BufferedReader ReceiveThread Reader.
EDIT:
, write, , , . , Activity, . , , , Activity, , . , public class MyActivity extends Activity. , Android "" , onReceive() onStartCommand() , , OutputStream .
, return Service.START_STICKY onStartCommand() . , write, , LocalBroadcastManager.