Guest User

Untitled

a guest
Apr 4th, 2014
182
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package huadi.androidgamepad;
  2.  
  3. import huadi.androidgamepad.Events.InputDevice;
  4. import huadi.androidgamepad.bluetooth.BleStatu;
  5. import huadi.androidgamepad.bluetooth.RFduinoUUIDs;
  6.  
  7. import java.util.Date;
  8. import java.util.UUID;
  9.  
  10. import android.app.Service;
  11. import android.bluetooth.BluetoothAdapter;
  12. import android.bluetooth.BluetoothDevice;
  13. import android.bluetooth.BluetoothGatt;
  14. import android.bluetooth.BluetoothGattCallback;
  15. import android.bluetooth.BluetoothGattCharacteristic;
  16. import android.bluetooth.BluetoothGattDescriptor;
  17. import android.bluetooth.BluetoothGattService;
  18. import android.bluetooth.BluetoothManager;
  19. import android.bluetooth.BluetoothProfile;
  20. import android.content.Context;
  21. import android.content.Intent;
  22. import android.os.Handler;
  23. import android.os.HandlerThread;
  24. import android.os.IBinder;
  25. import android.os.Looper;
  26. import android.os.Message;
  27. import android.util.Log;
  28. import android.widget.Toast;
  29.  
  30. public class MyService extends Service
  31. {
  32.     private final static String TAG = "RFduinoService";
  33.    
  34.     private Looper mServiceLooper;
  35.     private ServiceHandler mServiceHandler;
  36.    
  37.     Events events = new Events();
  38.     int id = 0;
  39.  
  40.     // Handler that receives messages from the thread
  41.     private final class ServiceHandler extends Handler
  42.     {
  43.         public ServiceHandler(Looper looper)
  44.         {
  45.             super(looper);
  46.         }
  47.  
  48.         @Override
  49.         public void handleMessage(Message msg)
  50.         {
  51.             // Normally we would do some work here, like download a file.
  52.             // For our sample, we just sleep for 5 seconds.
  53.             long endTime = System.currentTimeMillis() + 5 * 1000;
  54.             while (System.currentTimeMillis() < endTime)
  55.             {
  56.                 synchronized (this)
  57.                 {
  58.                     try
  59.                     {
  60.                         wait(1000);
  61.                         //events.m_Devs.get(id).SendTest(0x3be, 0x275);
  62.                         Log.e("time", new Date().toString());
  63.                         Log.e("readInfo", read());
  64.                     }
  65.                     catch (Exception e)
  66.                     {
  67.                     }
  68.                 }
  69.             }
  70.             // Stop the service using the startId, so that we don't stop
  71.             // the service in the middle of handling another job
  72.             //stopSelf(msg.arg1);
  73.         }
  74.     }
  75.  
  76.     @Override
  77.     public void onCreate()
  78.     {
  79.         Events.intEnableDebug(1);
  80.         events.Init();
  81.  
  82.         for (InputDevice idev : events.m_Devs)
  83.         {
  84.             idev.Open(true);
  85.             if (idev.getOpen() && idev.getName().contains("touch"))
  86.             {
  87.                 //Toast.makeText(this, "123", Toast.LENGTH_LONG).show();
  88.                 id = idev.getId();
  89.             }
  90.         }
  91.        
  92.         if(initialize())
  93.             if(connect(BleStatu.DeviceMacAddress))
  94.                 Log.e(TAG, "conn");
  95.        
  96.         // Start up the thread running the service.  Note that we create a
  97.         // separate thread because the service normally runs in the process's
  98.         // main thread, which we don't want to block.  We also make it
  99.         // background priority so CPU-intensive work will not disrupt our UI.
  100.         HandlerThread thread = new HandlerThread("ServiceStartArguments", android.os.Process.THREAD_PRIORITY_BACKGROUND);
  101.         thread.start();
  102.  
  103.         // Get the HandlerThread's Looper and use it for our Handler
  104.         mServiceLooper = thread.getLooper();
  105.         mServiceHandler = new ServiceHandler(mServiceLooper);
  106.     }
  107.  
  108.     @Override
  109.     public int onStartCommand(Intent intent, int flags, int startId)
  110.     {
  111.         Toast.makeText(this, "service starting", Toast.LENGTH_SHORT).show();
  112.  
  113.         // For each start request, send a message to start a job and deliver the
  114.         // start ID so we know which request we're stopping when we finish the job
  115.         Message msg = mServiceHandler.obtainMessage();
  116.         msg.arg1 = startId;
  117.         mServiceHandler.sendMessage(msg);
  118.  
  119.         // If we get killed, after returning from here, restart
  120.         return START_STICKY;
  121.     }
  122.  
  123.     @Override
  124.     public IBinder onBind(Intent intent)
  125.     {
  126.         // We don't provide binding, so return null
  127.         return null;
  128.     }
  129.  
  130.     @Override
  131.     public void onDestroy()
  132.     {
  133.         Toast.makeText(this, "service done", Toast.LENGTH_SHORT).show();
  134.     }
  135.    
  136.    
  137.    
  138.     private BluetoothManager mBluetoothManager;
  139.     private BluetoothAdapter mBluetoothAdapter;
  140.     private BluetoothGatt mBluetoothGatt;
  141.     private BluetoothGattService mBluetoothGattService;
  142.    
  143.  
  144.     private final BluetoothGattCallback mGattCallback = new BluetoothGattCallback()
  145.     {
  146.         @Override
  147.         public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState)
  148.         {
  149.             if (newState == BluetoothProfile.STATE_CONNECTED)
  150.             {
  151.                 Log.i(TAG, "Connected to RFduino.");
  152.                 Log.i(TAG, "Attempting to start service discovery:" + mBluetoothGatt.discoverServices());
  153.             }
  154.             else if (newState == BluetoothProfile.STATE_DISCONNECTED)
  155.             {
  156.                 Log.i(TAG, "Disconnected from RFduino.");
  157.             }
  158.             else {
  159.                 Log.e(TAG, "" + newState);
  160.             }
  161.         }
  162.  
  163.         @Override
  164.         public void onServicesDiscovered(BluetoothGatt gatt, int status)
  165.         {
  166.             if (status == BluetoothGatt.GATT_SUCCESS)
  167.             {
  168.                 mBluetoothGattService = gatt.getService(RFduinoUUIDs.RFduino_Service_UUID);
  169.                 if (mBluetoothGattService == null)
  170.                 {
  171.                     Log.e(TAG, "RFduino GATT service not found!");
  172.                     return;
  173.                 }
  174.  
  175.                 BluetoothGattCharacteristic receiveCharacteristic = mBluetoothGattService.getCharacteristic(RFduinoUUIDs.Receive_Characteristic_UUID);
  176.                 if (receiveCharacteristic != null)
  177.                 {
  178.                     BluetoothGattDescriptor receiveConfigDescriptor = receiveCharacteristic.getDescriptor(RFduinoUUIDs.Client_Characteristic_Config_UUID);
  179.                     if (receiveConfigDescriptor != null)
  180.                     {
  181.                         gatt.setCharacteristicNotification(receiveCharacteristic, true);
  182.  
  183.                         receiveConfigDescriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
  184.                         gatt.writeDescriptor(receiveConfigDescriptor);
  185.                        
  186.                         Log.e(TAG + 1, "" + BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
  187.                     }
  188.                     else
  189.                     {
  190.                         Log.e(TAG, "RFduino receive config descriptor not found!");
  191.                     }
  192.  
  193.                 }
  194.                 else
  195.                 {
  196.                     Log.e(TAG, "RFduino receive characteristic not found!");
  197.                 }
  198.             }
  199.             else
  200.             {
  201.                 Log.w(TAG, "onServicesDiscovered received: " + status);
  202.             }
  203.         }
  204.  
  205.         @Override
  206.         public void onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status)
  207.         {
  208.             if (status == BluetoothGatt.GATT_SUCCESS)
  209.             {
  210.                 Log.e(TAG, "a");
  211.             }
  212.         }
  213.  
  214.         @Override
  215.         public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic)
  216.         {
  217.             Log.e(TAG, "b");
  218.         }
  219.     };
  220.  
  221.  
  222.     public boolean initialize()
  223.     {
  224.         if (mBluetoothManager == null)
  225.         {
  226.             mBluetoothManager = (BluetoothManager) getSystemService(BLUETOOTH_SERVICE);
  227.             if (mBluetoothManager == null)
  228.             {
  229.                 Log.e(TAG, "Unable to initialize BluetoothManager.");
  230.                 return false;
  231.             }
  232.         }
  233.  
  234.         mBluetoothAdapter = mBluetoothManager.getAdapter();
  235.         if (mBluetoothAdapter == null)
  236.         {
  237.             Log.e(TAG, "Unable to obtain a BluetoothAdapter.");
  238.             return false;
  239.         }
  240.  
  241.         return true;
  242.     }
  243.  
  244.     public boolean connect(final String address)
  245.     {
  246.         if (mBluetoothAdapter == null || address == null)
  247.         {
  248.             Log.w(TAG, "BluetoothAdapter not initialized or unspecified address.");
  249.             return false;
  250.         }
  251.  
  252.         // Previously connected device.  Try to reconnect.
  253.         if (mBluetoothGatt != null)
  254.         {
  255.             Log.d(TAG, "Trying to use an existing mBluetoothGatt for connection.");
  256.             return mBluetoothGatt.connect();
  257.         }
  258.  
  259.         final BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(address);
  260.         // We want to directly connect to the device, so we are setting the autoConnect
  261.         // parameter to false.
  262.         mBluetoothGatt = device.connectGatt(this, true, mGattCallback);
  263.         Log.d(TAG, "Trying to create a new connection.");
  264.         return true;
  265.     }
  266.  
  267.     public void disconnect()
  268.     {
  269.         if (mBluetoothAdapter == null || mBluetoothGatt == null)
  270.         {
  271.             Log.w(TAG, "BluetoothAdapter not initialized");
  272.             return;
  273.         }
  274.         mBluetoothGatt.disconnect();
  275.     }
  276.  
  277.     public void close()
  278.     {
  279.         if (mBluetoothGatt == null)
  280.         {
  281.             return;
  282.         }
  283.         mBluetoothGatt.close();
  284.         mBluetoothGatt = null;
  285.     }
  286.  
  287.     public String read()
  288.     {
  289.         BluetoothGattService gattService = mBluetoothGatt.getService(UUID.fromString("00002220-0000-1000-8000-00805f9b34fb"));
  290.         if (mBluetoothGatt == null)
  291.         {
  292.             Log.w(TAG, "mBluetoothGatt not initialized");
  293.             return "mBluetoothGatt not initialized";
  294.         }
  295.         else if(gattService == null) //                            就是這裡!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!1
  296.         {
  297.             Log.w(TAG, "gattService not initialized");
  298.             return "gattService not initialized";
  299.         }
  300.  
  301.         BluetoothGattCharacteristic characteristic = gattService.getCharacteristic(RFduinoUUIDs.Receive_Characteristic_UUID);
  302.  
  303.         return "" + mBluetoothGatt.readCharacteristic(characteristic);
  304.     }
  305.  
  306.     public boolean send(byte[] data)
  307.     {
  308.         if (mBluetoothGatt == null || mBluetoothGattService == null)
  309.         {
  310.             Log.w(TAG, "BluetoothGatt not initialized");
  311.             return false;
  312.         }
  313.  
  314.         BluetoothGattCharacteristic characteristic = mBluetoothGattService.getCharacteristic(RFduinoUUIDs.Send_Characteristic_UUID);
  315.  
  316.         if (characteristic == null)
  317.         {
  318.             Log.w(TAG, "Send characteristic not found");
  319.             return false;
  320.         }
  321.  
  322.         characteristic.setValue(data);
  323.         characteristic.setWriteType(BluetoothGattCharacteristic.WRITE_TYPE_NO_RESPONSE);
  324.         return mBluetoothGatt.writeCharacteristic(characteristic);
  325.     }
  326.    
  327.  
  328. }
Advertisement
Add Comment
Please, Sign In to add comment