2016-04-04 11 views
0

すべてのBluetooth接続を処理するBluetoothアクティビティ(BTHandler)をサービスに移動する必要があるため、接続を失うことなく複数のアクティビティを使用できます。それをどうやってやりますか?アクティビティをサービスに移動する方法

おかげ

BTHandler

public class BTHandler { 

    public static final int STATE_NONE = 0;  // we're doing nothing 
    public static final int STATE_CONNECTING = 2; // now initiating an outgoing connection 
    public static final int STATE_CONNECTED = 3; // now connected to a remote device 

    final ArrayList<String> devices = new ArrayList(); 
    private final Handler mHandler; 
    private BluetoothAdapter mAdapter; 
    private BluetoothDevice device; 
    private ConnectThread mConnectThread; 
    private ConnectedThread mConnectedThread; 
    private BluetoothSocket socket; 
    private String status; 
    private int mState; 
    private boolean connectionStatus = false; 

    public BTHandler(Context context, Handler handler) { // Konstruktor 
     mAdapter = BluetoothAdapter.getDefaultAdapter(); 
     mHandler = handler; 
    } 

    public void write(String s) { 
     mConnectedThread.sendRawCommand(s); 
     Log.v("write", "write"); 
    } 

    /* 
    public void write(byte[] bytes) { 
     try { 
      mmOutStream.write(bytes); 
     } catch (IOException e) { 
     } 
    } 
    */ 
    public void connect(String deviceAddress) { 
     mConnectThread = new ConnectThread(deviceAddress); 
     mConnectThread.start(); 
    } 

    private void guiHandler(int what, int arg1, String obj) { 
     Message msg = mHandler.obtainMessage(); 
     msg.what = what; 
     msg.obj = obj; 
     msg.arg1 = arg1; 
     msg.sendToTarget(); 
    } 

    private class ConnectThread extends Thread { 
     BluetoothSocket tmp = null; 
     private BluetoothSocket mmSocket; 

     public ConnectThread(String deviceAddress) { 
      mAdapter = BluetoothAdapter.getDefaultAdapter(); 
      device = mAdapter.getRemoteDevice(deviceAddress); 

      BluetoothAdapter mAdapter = BluetoothAdapter.getDefaultAdapter(); 
      BluetoothDevice device = mAdapter.getRemoteDevice(deviceAddress); 
      UUID uuid = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"); 
      try { 
       tmp = device.createRfcommSocketToServiceRecord(uuid); 
       //socket.connect(); 
       //Log.v("connect", "connect"); 
      } catch (IOException e) { 
       //e.printStackTrace(); 
       //Log.v("exception", "e"); 
      } 
      mmSocket = tmp; 
     } 

     public void run() { 
      // Cancel discovery because it will slow down the connection 
      mAdapter.cancelDiscovery(); 
      byte[] buffer = new byte[1024]; // buffer store for the stream 
      int bytes; 

      try { 
       // Connect the device through the socket. This will block 
       // until it succeeds or throws an exception 
       mmSocket.connect(); 
       Log.v("connect", "connect"); 
      } catch (IOException connectException) { 
       // Unable to connect; close the socket and get out 
       try { 
        mmSocket.close(); 
        Log.v("close", "close"); 
       } catch (IOException closeException) { 
       } 
       guiHandler(Constants.TOAST, Constants.SHORT, "Connection Failed"); 
       return; 
      } 
      guiHandler(Constants.CONNECTION_STATUS, Constants.STATE_CONNECTED, ""); 
      mConnectedThread = new ConnectedThread(mmSocket); 
      mConnectedThread.start(); 
     } 
    } 

    private class ConnectedThread extends Thread { 
     private final BluetoothSocket mmSocket; 
     private final InputStream mmInStream; 
     private final OutputStream mmOutStream; 
     private ObdMultiCommand multiCommand; 

     public ConnectedThread(BluetoothSocket socket) { 
      connectionStatus = true; 
      mmSocket = socket; 
      InputStream tmpIn = null; 
      OutputStream tmpOut = null; 

      try { 
       tmpIn = socket.getInputStream(); 
       tmpOut = socket.getOutputStream(); 
      } catch (IOException e) { 
      } 

      mmInStream = tmpIn; 
      mmOutStream = tmpOut; 

      try { 
       //RPMCommand engineRpmCommand = new RPMCommand(); 
       //SpeedCommand speedCommand = new SpeedCommand(); 
       ModuleVoltageCommand voltageCommand = new ModuleVoltageCommand(); 

       while (!Thread.currentThread().isInterrupted()) { 
        //engineRpmCommand.run(mmInStream, mmOutStream); //(socket.getInputStream(), socket.getOutputStream()); 
        //speedCommand.run(mmInStream, mmOutStream); //(socket.getInputStream(), socket.getOutputStream()); 
        voltageCommand.run(socket.getInputStream(), socket.getOutputStream()); 
        // TODO handle commands result 
        //Log.d("Log", "RPM: " + engineRpmCommand.getFormattedResult()); 
        //Log.d("Log", "Speed: " + speedCommand.getFormattedResult()); 
        Log.v("Log", "Voltage: " + voltageCommand.getFormattedResult()); 
       } 
      } catch (Exception e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 

     } 

     public void run() { 
      byte[] buffer = new byte[1024]; // buffer store for the stream 
      int bytes; // bytes returned from read() 

      OBDcmds(); 
      // Keep listening to the InputStream until an exception occurs 
      while (connectionStatus) { 
       sendMultiCommand(); 
       try { 
        Thread.sleep(500); 
       } catch (InterruptedException e) { 
        e.printStackTrace(); 
       } 
      } 
     } 

     // CALL this to MainActivity 
     public void sendRawCommand(String command) { 
      try { 
       new OdbRawCommand(command); 

      } catch (Exception e) { 
       Log.v("sendRawCommand", "e"); 
      } 
     } 

     private void OBDcmds() { // execute commands 

      try { 
       new EchoOffCommand().run(socket.getInputStream(), socket.getOutputStream()); 
       new LineFeedOffCommand().run(socket.getInputStream(), socket.getOutputStream()); 
       new TimeoutCommand(100).run(socket.getInputStream(), socket.getOutputStream()); 
       new SelectProtocolCommand(ObdProtocols.AUTO).run(socket.getInputStream(), socket.getOutputStream()); //ISO_15765_4_CAN 

      } catch (Exception e) { 
       Log.v("OBDcmds", "e"); 
       // handle errors 
      } 
     } 
     /* 
     // Call this from the main activity to send data to the remote device 
     public void write(byte[] bytes) { 
      try { 
       mmOutStream.write(bytes); 
      } catch (IOException e) { 
      } 
     } 
     */ 

     /* Call this from the main activity to shutdown the connection */ 
     public void cancel() { 
      try { 
       mmSocket.close(); 
      } catch (IOException e) { 
      } 
     } 

     public void sendMultiCommand() { 
      try { 
       // RUN some code here 
      } catch (Exception e) { 
      } 
     } 
    } 
} 

あなたはサービス内のスレッドとスタッフのあなたの変数を作成する必要がMyServiceで

public class MyService extends Service { 
    public MyService() { 
    } 

    @Override 
    public IBinder onBind(Intent intent) { 
     return null; 
    } 

    @Override 
    public void onCreate() { 
     Toast.makeText(this, "The new Service was Created", Toast.LENGTH_LONG).show(); 

    } 

    @Override 
    public void onStart(Intent intent, int startId) { 
     // For time consuming an long tasks you can launch a new thread here... 
     // Do your Bluetooth Work Here 
     Toast.makeText(this, " Service Started", Toast.LENGTH_LONG).show(); 

    } 

    @Override 
    public void onDestroy() { 
     Toast.makeText(this, "Service Destroyed", Toast.LENGTH_LONG).show(); 

    } 
} 
+0

"バインドされたローカルサービス"パターン – pskink

+0

を読んでいますが、コードをサービスに転送する方法がわかりません。 'onBind'やサービス中の他のメソッドに入るべきもののように。 – swess

+0

http://developer.android.com/guide/components/bound-services.html#Binderを参照してください – pskink

答えて

0

+0

私に例を教えてください。 – swess

+0

ここをクリック:http://stackoverflow.com/questions/15025852/how-to-move-bluetooth-activity-into-a-service – jazzyjester

関連する問題