How to access the methods of my Bluetooth service?

0

I have an application that from the MainAcivity increases or excretes a int Grados depending on which button is pressed, the add or subtract.

Since BluetoothActivity turned on and off the Bluetooth and launched a service that handles the bluetooth connection with any device, the problem is that I do not know how to send from Bluetooth activity or from MainActivity that variable int Grados .

This class is in a BluetoothService extends Service that is launched with

Intent intentBTService = new Intent(BluetoothActivity.this,BluetoothService_Test.class);
            intentBTService.putExtra("device_address",address);
            startService(intentBTService);

Lega to onStarCommand

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    //Log.d(TAG, "Servicio iniciado...")
    //if(intent != null){}
    Message msg = mServiceHandler.obtainMessage();
    msg.arg1 = startId;
    msg.obj = intent;
    mServiceHandler.sendMessage(msg);
    return START_NOT_STICKY;
}

And from there to a handler that handles the process in background

private final class CountHandler extends Handler{
    CountHandler(Looper looper){
        super(looper);
    }
    @Override
    public void handleMessage(Message msg){
        Intent request = (Intent) msg.obj;
        String address = request.getStringExtra("device_address");
        int starId =msg.arg1;
        if (address != null){
            mBluetoothAdapter =BluetoothAdapter.getDefaultAdapter();
            mBTDevice = mBluetoothAdapter.getRemoteDevice(address);
            connect(mBTDevice);

        }
    }
}

@Override
public void onCreate() {
    super.onCreate();
    HandlerThread backgroundThread  = new HandlerThread("CounterThread",
            Process.THREAD_PRIORITY_BACKGROUND);
    backgroundThread .start();
    mServiceHandler =  new CountHandler(backgroundThread.getLooper());
    localBroadcastManager = LocalBroadcastManager.getInstance(getApplicationContext());
}

This is the class where reading and writing are handled

private class ConnectedThread extends Thread{
   // private final BluetoothSocket mmSocket;
    private final InputStream mmInStream;
    private final OutputStream mmOutStream;
    public ConnectedThread(BluetoothSocket socket){

        mBTSocket = socket;
        //mmSocket = socket;
        InputStream tempIn = null;
        OutputStream temOut = null;

        try {
            tempIn = mBTSocket.getInputStream();
            temOut = mBTSocket.getOutputStream();
        } catch (IOException e) {
            e.printStackTrace();
        }
        mmInStream = tempIn;
        mmOutStream = temOut;
        mState = S_STATE_CONNECTED;


        mState = S_STATE_CONNECTED;
        String nameDevice = mBTDevice.getName();
        String nameAddress = mBTDevice.getAddress();
        Intent resultIntent = new Intent(ACTION_BT_SERVICE_CONNECTED);
        resultIntent.putExtra("addressDevice",nameAddress);
        resultIntent.putExtra("nameDevice",nameDevice);
        localBroadcastManager.sendBroadcast(resultIntent);
    }

    public void run(){
        byte[] buffer = new byte[1024];
        int bytes;

        while (mState == S_STATE_CONNECTED ){
            try {
                // Read from the InputStream
                bytes = mmInStream.read(buffer);
                //bytes = mmInStream.available();
                // Send the obtained bytes to the UI Activity
                if(bytes != 0) {
                    //Mensaje recibido
                    //mServiceHandler.obtainMessage(STATE_MESSAGE_RECIVED,bytes,-1,buffer).sendToTarget();
                }
            } catch (IOException e) {
                e.printStackTrace();
                connectionLost();
                break;
            }
        }
    }
    public void write(byte[] bytes ) {
                //converts entered String into bytes
        try {
            mmOutStream.write(bytes);
            //mmOutStream.write(bytes);
        } catch (IOException e) {
            connectionFailed();
        }
    }
    //Call this from the main activity to shutdown the connection
    public void cancel() {
        try {
            mBTSocket.close();

        } catch (IOException e) {

        }
    }
}

Inside connect are the other methods to connect the socket and others, what I want is to know how to access the write method of ConnectedThread from an activity to send my int Grados to an arduino for example

    
asked by Alejandro C. Bonilla 12.10.2018 в 23:16
source

0 answers