I'm developing an app to control LED. I want to send String
value from multiple Activity
so I'm using Android Service to run Bluetooth connection in background. I don't no how to send value from multiple activity to control LED.
Here's the BluetoothDataService
class
public class BluetoothDataService extends Service {
final int handlerState = 0;
Handler bluetoothIn;
private BluetoothAdapter btAdapter = null;
private ConnectingThread mConnectingThread;
private ConnectedThread mConnectedThread;
private boolean stopThread;
private static final UUID BTMODULEUUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
private BluetoothSocket mmSocket;
private static String MAC_ADDRESS="98:D3:31:30:A5:00" ;
private final IBinder mBinder = (IBinder) new LocalBinder();
private StringBuilder recDataString = new StringBuilder();
private String on="";
@Override
public void onCreate() {
super.onCreate();
Log.d("BT SERVICE", "SERVICE CREATED");
stopThread = false;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
super.onStartCommand(intent, flags,startId);
Log.d("BT SERVICE", "SERVICE STARTED");
bluetoothIn = new Handler()
{
public void handleMessage(android.os.Message msg) {
Log.d("DEBUG", "handleMessage");
if (msg.what == handlerState) {
String readMessage = (String) msg.obj;
recDataString.append(readMessage);
// mConnectedThread.write(on);
Log.d("RECORDED", recDataString.toString());
}
recDataString.delete(0, recDataString.length());
}
};
btAdapter = BluetoothAdapter.getDefaultAdapter();
checkBTState();
return START_STICKY;
}
@Override
public void onDestroy() {
super.onDestroy();
bluetoothIn.removeCallbacksAndMessages(null);
stopThread = true;
if (mConnectedThread != null ) {
mConnectedThread.closeStreams();
}
if (mConnectingThread != null) {
mConnectingThread.closeSocket();
}
Log.d("SERVICE", "onDestroy");
}
@Override
public IBinder onBind(Intent intent) {
Log.v(" TEST", "service ONBIND");
return mBinder;
}
//Checks Bluetooth is available and turned on /off
private void checkBTState() {
if (btAdapter == null) {
Log.d("BT SERVICE", "BLUETOOTH NOT SUPPORTED BY DEVICE, STOPPING SERVICE");
stopSelf();
} else {
if (btAdapter.isEnabled()) {
Log.d("DEBUG BT", "BT ENABLED! BT ADDRESS : " + btAdapter.getAddress() + " , BT NAME : " + btAdapter.getName());
try {
BluetoothDevice device = btAdapter.getRemoteDevice(MAC_ADDRESS);
Log.d("DEBUG BT", "ATTEMPTING TO CONNECT TO REMOTE DEVICE : " + MAC_ADDRESS);
mConnectingThread = new ConnectingThread(device);
mConnectingThread.start();
} catch (IllegalArgumentException e) {
Log.d("DEBUG BT", "PROBLEM WITH MAC ADDRESS : " + e.toString());
Log.d("BT SEVICE", "ILLEGAL MAC ADDRESS, STOPPING SERVICE");
stopSelf();
}
} else {
Log.d("BT SERVICE", "BLUETOOTH NOT ON, STOPPING SERVICE");
stopSelf();
}
}
}
public class LocalBinder extends Binder {
BluetoothDataService getService() {
return BluetoothDataService.this;
}
}
// New Class for Connecting Thread
private class ConnectingThread extends Thread {
// private final BluetoothSocket mmSocket;
private final BluetoothDevice mmDevice;
public ConnectingThread(BluetoothDevice device) {
Log.d("DEBUG BT", "IN CONNECTING THREAD");
mmDevice = device;
BluetoothSocket temp = null;
Log.d("DEBUG BT", "MAC ADDRESS : " + MAC_ADDRESS);
Log.d("DEBUG BT", "BT UUID : " + BTMODULEUUID);
try {
temp = mmDevice.createRfcommSocketToServiceRecord(BTMODULEUUID);
Log.d("DEBUG BT", "SOCKET CREATED : " + temp.toString());
} catch (IOException e) {
Log.d("DEBUG BT", "SOCKET CREATION FAILED :" + e.toString());
Log.d("BT SERVICE", "SOCKET CREATION FAILED, STOPPING SERVICE");
stopSelf();
}
mmSocket = temp;
}
@Override
public void run() {
super.run();
Log.d("DEBUG BT", "IN CONNECTING THREAD RUN");
btAdapter.cancelDiscovery();
try {
mmSocket.connect();
Log.d("DEBUG BT", "BT SOCKET CONNECTED");
mConnectedThread = new ConnectedThread(mmSocket);
mConnectedThread.start();
Log.d("DEBUG BT", "CONNECTED THREAD STARTED");
mConnectedThread.write("x");
} catch (IOException e) {
try {
Log.d("DEBUG BT", "SOCKET CONNECTION FAILED : " + e.toString());
Log.d("BT SERVICE", "SOCKET CONNECTION FAILED, STOPPING SERVICE");
mmSocket.close();
stopSelf();
}
catch (IOException e2) {
Log.d("DEBUG BT", "SOCKET CLOSING FAILED :" + e2.toString());
Log.d("BT SERVICE", "SOCKET CLOSING FAILED, STOPPING SERVICE");
stopSelf();
}
} catch (IllegalStateException e) {
Log.d("DEBUG BT", "CONNECTED THREAD START FAILED : " + e.toString());
Log.d("BT SERVICE", "CONNECTED THREAD START FAILED, STOPPING SERVICE");
stopSelf();
}
}
public void closeSocket() {
try {
mmSocket.close();
} catch (IOException e2) {
Log.d("DEBUG BT", e2.toString());
Log.d("BT SERVICE", "SOCKET CLOSING FAILED, STOPPING SERVICE");
stopSelf();
}
}
}
// New Class for Connected Thread
public class ConnectedThread extends Thread {
private final InputStream mmInStream;
private final OutputStream mmOutStream;
//creation of the connect thread
public ConnectedThread(BluetoothSocket socket) {
Log.d("DEBUG BT", "IN CONNECTED THREAD");
InputStream tmpIn = null;
OutputStream tmpOut = null;
try {
//Create I/O streams for connection
tmpIn = socket.getInputStream();
tmpOut = socket.getOutputStream();
} catch (IOException e) {
Log.d("DEBUG BT", e.toString());
Log.d("BT SERVICE", "UNABLE TO READ/WRITE, STOPPING SERVICE");
stopSelf();
}
mmInStream = tmpIn;
mmOutStream = tmpOut;
}
public void run() {
Log.d("DEBUG BT", "IN CONNECTED THREAD RUN");
byte[] buffer = new byte[256];
// write(on);
int bytes;
// Keep looping to listen for received messages
while (true && !stopThread) {
try {
bytes = mmInStream.read(buffer);
String readMessage = new String(buffer, 0, bytes);
Log.d("DEBUG BT PART", "CONNECTED THREAD " + readMessage);
// Send the obtained bytes to the UI Activity via handler
bluetoothIn.obtainMessage(handlerState, bytes, -1, readMessage).sendToTarget();
} catch (IOException e) {
Log.d("DEBUG BT", e.toString());
Log.d("BT SERVICE", "UNABLE TO READ/WRITE, STOPPING SERVICE");
stopSelf();
break;
}
}
}
//write method
public void write(String input) {
byte[] msgBuffer = input.getBytes();
try {
mmOutStream.write(msgBuffer);
} catch (IOException e) {
Log.d("DEBUG BT", "UNABLE TO READ/WRITE " + e.toString());
Log.d("BT SERVICE", "UNABLE TO READ/WRITE, STOPPING SERVICE");
stopSelf();
}
}
public void closeStreams() {
try {
mmInStream.close();
mmOutStream.close();
} catch (IOException e2)
{
Log.d("DEBUG BT", e2.toString());
Log.d("BT SERVICE", "STREAM CLOSING FAILED, STOPPING SERVICE");
stopSelf();
}
}
}
I'm sending value from multiple Activity
to BluetoothDataService
class to control LED. In my code I want to send value when user click the button.
MY FIRST ACTIVITY
public class SendActivity extends Activity implements View.OnClickListener {
Button on;
private BluetoothDataService bluetoothDataService;
public boolean isBound = false;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_send);
on= (Button) findViewById(R.id.btnon);
on.setOnClickListener(this);
Intent intent=new Intent (SendActivity.this,BluetoothDataService.class);
startService(intent);
}
@Override
public void onStart() {
super.onStart();
Intent intent = new Intent(this, BluetoothDataService.class);
bindService(intent, connection, Context.BIND_AUTO_CREATE);
Log.v("BINDING", "BIND");
}
private ServiceConnection connection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName className, IBinder service) {
BluetoothDataService.LocalBinder binder = (BluetoothDataService.LocalBinder) service;
bluetoothDataService = binder.getService();
isBound = true;
// BluetoothDataService.ConnectedThread.write("");
Log.v("ServiceConnection","ServiceConnection");
}
@Override
public void onServiceDisconnected(ComponentName arg0) {
isBound = false;
Log.v("ServiceDisConnected","ServiDisceConneted");
}
};
@Override
public void onClick(View v) {
if (isBound)
{
Intent intent=new Intent (SendActivity.this,BluetoothDataService.class);
intent.putExtra("ON", "#ffffff00");
startService(intent);
}
}
@Override
public void onStop() {
super.onStop();
if (isBound) {
Log.v("ServiceStop","ServiceStop");
unbindService(connection);
isBound = false;
}
}
LOG
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:754)
12-22 09:17:21.275 15645-15645/com.example.btest V/BINDING: BIND
12-22 09:17:21.275 15645-15645/com.example.btest V/ActivityThread: Performing resume of ActivityRecord{3fa5e5b5 token=android.os.BinderProxy@49e244a {com.example.btest/com.example.btest.SendActivity}}
12-22 09:17:21.275 15645-15645/com.example.btest D/ActivityThread: ACT-AM_ON_RESUME_CALLED ActivityRecord{3fa5e5b5 token=android.os.BinderProxy@49e244a {com.example.btest/com.example.btest.SendActivity}}
12-22 09:17:21.276 15645-15645/com.example.btest V/ActivityThread: Resume ActivityRecord{3fa5e5b5 token=android.os.BinderProxy@49e244a {com.example.btest/com.example.btest.SendActivity}} started activity: false, hideForNow: false, finished: false
12-22 09:17:21.277 15645-15645/com.example.btest V/PhoneWindow: DecorView setVisiblity: visibility = 4 ,Parent =null, this =com.android.internal.policy.impl.PhoneWindow$DecorView{2bdd8e84 I.E..... R.....ID 0,0-0,0}
12-22 09:17:21.293 15645-15676/com.example.btest D/OpenGLRenderer: initialize DisplayEventReceiver 0x7f82818140
12-22 09:17:21.294 15645-15676/com.example.btest D/OpenGLRenderer: Use EGL_SWAP_BEHAVIOR_PRESERVED: false
12-22 09:17:21.338 15645-15645/com.example.btest D/GraphicBuffer: register, handle(0x7f88b54e80) (w:768 h:768 s:768 f:0x1 u:0x000100)
12-22 09:17:21.341 15645-15645/com.example.btest D/Atlas: Validating map...
12-22 09:17:21.348 15645-15645/com.example.btest D/ViewRootImpl: hardware acceleration is enabled, this = ViewRoot{3e674f4d com.example.btest/com.example.btest.SendActivity,ident = 0}
12-22 09:17:21.355 15645-15645/com.example.btest V/ActivityThread: Resuming ActivityRecord{3fa5e5b5 token=android.os.BinderProxy@49e244a {com.example.btest/com.example.btest.SendActivity}} with isForward=true
12-22 09:17:21.355 15645-15645/com.example.btest D/FeatureProxyBase: FeatureProxyBase class constructor
12-22 09:17:21.355 15645-15645/com.example.btest D/MultiWindow: MultiWindowProxy constructor.
12-22 09:17:21.356 15645-15645/com.example.btest D/FeatureProxyBase: getService(), serviceName = multiwindow_service_v1
12-22 09:17:21.359 15645-15645/com.example.btest V/ActivityThread: Scheduling idle handler for ActivityRecord{3fa5e5b5 token=android.os.BinderProxy@49e244a {com.example.btest/com.example.btest.SendActivity}}
12-22 09:17:21.360 15645-15645/com.example.btest D/ActivityThread: ACT-LAUNCH_ACTIVITY handled : 0 / ActivityRecord{3fa5e5b5 token=android.os.BinderProxy@49e244a {com.example.btest/com.example.btest.SendActivity}}
12-22 09:17:21.361 15645-15645/com.example.btest D/ActivityThread: SVC-Creating service: CreateServiceData{token=android.os.BinderProxy@1aa9ef13 className=com.example.btest.BluetoothDataService packageName=com.example.btest intent=null}
12-22 09:17:21.361 15645-15645/com.example.btest D/ActivityThread: SVC-CREATE_SERVICE handled : 0 / CreateServiceData{token=android.os.BinderProxy@1aa9ef13 className=com.example.btest.BluetoothDataService packageName=com.example.btest intent=null}
12-22 09:17:21.359 15645-15645/com.example.btest V/PhoneWindow: DecorView setVisiblity: visibility = 0 ,Parent =ViewRoot{3e674f4d com.example.btest/com.example.btest.SendActivity,ident = 0}, this =com.android.internal.policy.impl.PhoneWindow$DecorView{2bdd8e84 V.E..... R.....ID 0,0-0,0}
12-22 09:17:21.361 15645-15645/com.example.btest D/BT SERVICE: SERVICE CREATED
12-22 09:17:21.362 15645-15645/com.example.btest D/ActivityThread: SVC-Calling onStartCommand: com.example.btest.BluetoothDataService@1ce4c550, flags=0, startId=1
12-22 09:17:21.362 15645-15645/com.example.btest D/BT SERVICE: SERVICE STARTED
12-22 09:17:21.388 15645-15645/com.example.btest D/BluetoothAdapter: isEnabled
12-22 09:17:21.390 15645-15645/com.example.btest D/BluetoothAdapter: getAddress
12-22 09:17:21.394 15645-15645/com.example.btest D/BluetoothAdapter: getName
12-22 09:17:21.405 15645-15645/com.example.btest D/DEBUG BT: BT ENABLED! BT ADDRESS : CC:5F:BF:3C:D6:5C , BT NAME : Micromax P701
12-22 09:17:21.405 15645-15645/com.example.btest D/DEBUG BT: ATTEMPTING TO CONNECT TO REMOTE DEVICE : 98:D3:32:20:42:54
12-22 09:17:21.406 15645-15645/com.example.btest D/DEBUG BT: IN CONNECTING THREAD
12-22 09:17:21.406 15645-15645/com.example.btest D/DEBUG BT: MAC ADDRESS : 98:D3:32:20:42:54
12-22 09:17:21.406 15645-15645/com.example.btest D/DEBUG BT: BT UUID : 00001101-0000-1000-8000-00805f9b34fb
12-22 09:17:21.411 15645-15645/com.example.btest D/BluetoothDevice: mAddress: 98:D3:32:20:42:54
12-22 09:17:21.419 15645-15645/com.example.btest D/DEBUG BT: SOCKET CREATED : android.bluetooth.BluetoothSocket@2d669349
12-22 09:17:21.420 15645-15645/com.example.btest D/ActivityThread: SVC-SERVICE_ARGS handled : 0 / ServiceArgsData{token=android.os.BinderProxy@1aa9ef13 startId=1 args=Intent { cmp=com.example.btest/.BluetoothDataService }}
12-22 09:17:21.421 15645-15645/com.example.btest V/ TEST: service ONBIND
12-22 09:17:21.430 15645-15681/com.example.btest D/DEBUG BT: IN CONNECTING THREAD RUN
12-22 09:17:21.431 15645-15645/com.example.btest D/ActivityThread: SVC-BIND_SERVICE handled : 0 / BindServiceData{token=android.os.BinderProxy@1aa9ef13 intent=Intent { cmp=com.example.btest/.BluetoothDataService }}
12-22 09:17:21.433 15645-15681/com.example.btest D/BluetoothAdapter: cancelDiscovery
12-22 09:17:21.436 15645-15681/com.example.btest D/BluetoothAdapter: 151200846: getState(). Returning 12
12-22 09:17:21.444 15645-15681/com.example.btest W/BluetoothAdapter: getBluetoothService() called with no BluetoothManagerCallback
12-22 09:17:21.502 15645-15676/com.example.btest D/OpenGLRenderer: CanvasContext() 0x7f82825a00 initialize 0x7f88a40a10
12-22 09:17:21.504 15645-15645/com.example.btest V/ServiceConnection: ServiceConnection
12-22 09:17:21.510 15645-15676/com.example.btest E/GED: Failed to get GED Log Buf, err(0)
12-22 09:17:21.510 15645-15676/com.example.btest I/OpenGLRenderer: Initialized EGL, version 1.4
12-22 09:17:21.510 15645-15676/com.example.btest D/MALI: eglCreateContext:206: [MALI] eglCreateContext display 0x7f8885f980, share context 0x0 here.
12-22 09:17:21.535 15645-15676/com.example.btest W/MALI: gles_context_new:222: [MALI]ctx init(phase:7) takes more than 20ms here. Elapse time(us): 20956
12-22 09:17:21.537 15645-15676/com.example.btest D/MALI: gles_context_new:248: Create GLES ctx 0x7f828ef008 successfully
12-22 09:17:21.537 15645-15676/com.example.btest D/MALI: eglCreateContext:543: [MALI] eglCreateContext end. Created context 0x7f828df940 here.
12-22 09:17:21.539 15645-15676/com.example.btest I/OpenGLRenderer: Initializing program atlas...
12-22 09:17:21.540 15645-15676/com.example.btest D/ProgramBinary/Service: BpProgramBinaryService.getFileDescriptor
12-22 09:17:21.541 15645-15676/com.example.btest D/ProgramBinary/Service: BpProgramBinaryService.getProgramMapLen
12-22 09:17:21.542 15645-15676/com.example.btest D/ProgramBinary/Service: BpProgramBinaryService.getProgramMapArray
12-22 09:17:21.542 15645-15676/com.example.btest D/ProgramBinary/Service: BpProgramBinaryService.getProgramBinaryLen
12-22 09:17:21.542 15645-15676/com.example.btest I/OpenGLRenderer: Program binary detail: Binary length is 120912, program map length is 112.
12-22 09:17:21.542 15645-15676/com.example.btest I/OpenGLRenderer: Succeeded to mmap program binaries. File descriptor is 42, and path is /dev/ashmemt.
12-22 09:17:21.543 15645-15676/com.example.btest I/OpenGLRenderer: No need to use file discriptor anymore, close fd(42).
12-22 09:17:21.543 15645-15676/com.example.btest D/OpenGLRenderer: TaskManager() 0x7f828b01e8, cpu = 4, thread = 2
12-22 09:17:21.544 15645-15676/com.example.btest D/OpenGLRenderer: Enabling debug mode 0
12-22 09:17:21.545 15645-15676/com.example.btest D/Surface: Surface::connect(this=0x7f88a40a00,api=1)
12-22 09:17:21.547 15645-15676/com.example.btest D/mali_winsys: new_window_surface returns 0x3000
12-22 09:17:21.547 15645-15676/com.example.btest D/Surface: Surface::allocateBuffers(this=0x7f88a40a00)
12-22 09:17:21.586 15645-15656/com.example.btest W/art: Suspending all threads took: 33.517ms
12-22 09:17:21.620 15645-15676/com.example.btest D/Surface: Surface::setBuffersDimensions(this=0x7f88a40a00,w=976,h=600)
12-22 09:17:21.623 15645-15676/com.example.btest D/GraphicBuffer: register, handle(0x7f82abfb00) (w:976 h:600 s:976 f:0x1 u:0x000b00)
12-22 09:17:21.628 15645-15676/com.example.btest W/MALI: glDrawArrays:714: [MALI] glDrawArrays takes more than 5ms here. Total elapse time(us): 8107
12-22 09:17:21.657 15645-15645/com.example.btest V/InputMethodManager: onWindowFocus: null softInputMode=288 first=true flags=#1810100
12-22 09:17:21.658 15645-15645/com.example.btest V/InputMethodManager: START INPUT: com.android.internal.policy.impl.PhoneWindow$DecorView{2bdd8e84 V.E..... R......D 0,0-600,976} ic=null tba=android.view.inputmethod.EditorInfo@1c7b6f26 controlFlags=#104
12-22 09:17:21.936 15645-15676/com.example.btest D/Surface: Surface::setBuffersDimensions(this=0x7f88a40a00,w=976,h=600)
12-22 09:17:21.939 15645-15676/com.example.btest D/GraphicBuffer: register, handle(0x7f88b55560) (w:976 h:600 s:976 f:0x1 u:0x000b00)
12-22 09:17:22.124 15645-15681/com.example.btest D/DEBUG BT: BT SOCKET CONNECTED
12-22 09:17:22.124 15645-15681/com.example.btest D/DEBUG BT: IN CONNECTED THREAD
12-22 09:17:22.126 15645-15681/com.example.btest D/DEBUG BT: CONNECTED THREAD STARTED
12-22 09:17:22.130 15645-15720/com.example.btest D/DEBUG BT: IN CONNECTED THREAD RUN
12-22 09:17:37.752 15645-15720/com.example.btest D/DEBUG BT: java.io.IOException: bt socket closed, read return: -1
12-22 09:17:37.752 15645-15720/com.example.btest D/BT SERVICE: UNABLE TO READ/WRITE, STOPPING SERVICE