Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Apr 16th, 2012  |  syntax: None  |  size: 9.38 KB  |  hits: 14  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Android HDP compilation error and getting an error message source not found
  2. public static final int RESULT_OK = 0;
  3. public static final int RESULT_FAIL = -1;
  4.  
  5. // Status codes sent back to the UI client.
  6. // Application registration complete.
  7. public static final int STATUS_HEALTH_APP_REG = 100;
  8. // Application unregistration complete.
  9. public static final int STATUS_HEALTH_APP_UNREG = 101;
  10. // Channel creation complete.
  11. public static final int STATUS_CREATE_CHANNEL = 102;
  12. // Channel destroy complete.
  13. public static final int STATUS_DESTROY_CHANNEL = 103;
  14. // Reading data from Bluetooth HDP device.
  15. public static final int STATUS_READ_DATA = 104;
  16. // Done with reading data.
  17. public static final int STATUS_READ_DATA_DONE = 105;
  18.  
  19. // Message codes received from the UI client.
  20. // Register client with this service.
  21. public static final int MSG_REG_CLIENT = 200;
  22. // Unregister client from this service.
  23. public static final int MSG_UNREG_CLIENT = 201;
  24. // Register health application.
  25. public static final int MSG_REG_HEALTH_APP = 300;
  26. // Unregister health application.
  27. public static final int MSG_UNREG_HEALTH_APP = 301;
  28. // Connect channel.
  29. public static final int MSG_CONNECT_CHANNEL = 400;
  30. // Disconnect channel.
  31. public static final int MSG_DISCONNECT_CHANNEL = 401;
  32.  
  33. private BluetoothHealthAppConfiguration mHealthAppConfig;
  34. private BluetoothAdapter mBluetoothAdapter;
  35. private BluetoothHealth mBluetoothHealth;
  36. private BluetoothDevice mDevice;
  37. private int mChannelId;
  38.  
  39. private Messenger mClient;
  40.  
  41. // Handles events sent by {@link HealthHDPActivity}.
  42. private class IncomingHandler extends Handler {
  43.     @Override
  44.     public void handleMessage(Message msg) {
  45.         switch (msg.what) {
  46.             // Register UI client to this service so the client can receive messages.
  47.             case MSG_REG_CLIENT:
  48.                 Log.d(TAG, "Activity client registered");
  49.                 mClient = msg.replyTo;
  50.                 break;
  51.             // Unregister UI client from this service.
  52.             case MSG_UNREG_CLIENT:
  53.                 mClient = null;
  54.                 break;
  55.             // Register health application.
  56.             case MSG_REG_HEALTH_APP:
  57.                 registerApp(msg.arg1);
  58.                 break;
  59.             // Unregister health application.
  60.             case MSG_UNREG_HEALTH_APP:
  61.                 unregisterApp();
  62.                 break;
  63.             // Connect channel.
  64.             case MSG_CONNECT_CHANNEL:
  65.                 mDevice = (BluetoothDevice) msg.obj;
  66.                 connectChannel();
  67.                 break;
  68.             // Disconnect channel.
  69.             case MSG_DISCONNECT_CHANNEL:
  70.                 mDevice = (BluetoothDevice) msg.obj;
  71.                 disconnectChannel();
  72.                 break;
  73.             default:
  74.                 super.handleMessage(msg);
  75.         }
  76.     }
  77. }
  78.  
  79. final Messenger mMessenger = new Messenger(new IncomingHandler());
  80.  
  81. /**
  82.  * Make sure Bluetooth and health profile are available on the Android device.  Stop service
  83.  * if they are not available.
  84.  */
  85. @Override
  86. public void onCreate() {
  87.     super.onCreate();
  88.     mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
  89.     if (mBluetoothAdapter == null || !mBluetoothAdapter.isEnabled()) {
  90.         // Bluetooth adapter isn't available.  The client of the service is supposed to
  91.         // verify that it is available and activate before invoking this service.
  92.         stopSelf();
  93.         return;
  94.     }
  95.     if (!mBluetoothAdapter.getProfileProxy(this, mBluetoothServiceListener,
  96.             BluetoothProfile.HEALTH)) {
  97.         Toast.makeText(this, R.string.bluetooth_health_profile_not_available,
  98.                 Toast.LENGTH_LONG);
  99.         stopSelf();
  100.         return;
  101.     }
  102. }
  103.  
  104. @Override
  105. public int onStartCommand(Intent intent, int flags, int startId) {
  106.     Log.d(TAG, "BluetoothHDPService is running.");
  107.     return START_STICKY;
  108. }
  109.  
  110. @Override
  111. public IBinder onBind(Intent intent) {
  112.     return mMessenger.getBinder();
  113. };
  114.  
  115. // Register health application through the Bluetooth Health API.
  116. private void registerApp(int dataType) {
  117.     mBluetoothHealth.registerSinkAppConfiguration(TAG, dataType, mHealthCallback);
  118. }
  119.  
  120. // Unregister health application through the Bluetooth Health API.
  121. private void unregisterApp() {
  122.     mBluetoothHealth.unregisterAppConfiguration(mHealthAppConfig);
  123. }
  124.  
  125. // Connect channel through the Bluetooth Health API.
  126. private void connectChannel() {
  127.     Log.i(TAG, "connectChannel()");
  128.     mBluetoothHealth.connectChannelToSource(mDevice, mHealthAppConfig);
  129. }
  130.  
  131. // Disconnect channel through the Bluetooth Health API.
  132. private void disconnectChannel() {
  133.     Log.i(TAG, "disconnectChannel()");
  134.     mBluetoothHealth.disconnectChannel(mDevice, mHealthAppConfig, mChannelId);
  135. }
  136.  
  137. // Callbacks to handle connection set up and disconnection clean up.
  138. private final BluetoothProfile.ServiceListener mBluetoothServiceListener =
  139.         new BluetoothProfile.ServiceListener() {
  140.     public void onServiceConnected(int profile, BluetoothProfile proxy) {
  141.         if (profile == BluetoothProfile.HEALTH) {
  142.             mBluetoothHealth = (BluetoothHealth) proxy;
  143.             if (Log.isLoggable(TAG, Log.DEBUG))
  144.                 Log.d(TAG, "onServiceConnected to profile: " + profile);
  145.         }
  146.     }
  147.  
  148.     public void onServiceDisconnected(int profile) {
  149.         if (profile == BluetoothProfile.HEALTH) {
  150.             mBluetoothHealth = null;
  151.         }
  152.     }
  153. };
  154.  
  155. private final BluetoothHealthCallback mHealthCallback = new BluetoothHealthCallback() {
  156.     // Callback to handle application registration and unregistration events.  The service
  157.     // passes the status back to the UI client.
  158.     public void onHealthAppConfigurationStatusChange(BluetoothHealthAppConfiguration config,
  159.             int status) {
  160.         if (status == BluetoothHealth.APP_CONFIG_REGISTRATION_FAILURE) {
  161.             mHealthAppConfig = null;
  162.             sendMessage(STATUS_HEALTH_APP_REG, RESULT_FAIL);
  163.         } else if (status == BluetoothHealth.APP_CONFIG_REGISTRATION_SUCCESS) {
  164.             mHealthAppConfig = config;
  165.             sendMessage(STATUS_HEALTH_APP_REG, RESULT_OK);
  166.         } else if (status == BluetoothHealth.APP_CONFIG_UNREGISTRATION_FAILURE ||
  167.                 status == BluetoothHealth.APP_CONFIG_UNREGISTRATION_SUCCESS) {
  168.             sendMessage(STATUS_HEALTH_APP_UNREG,
  169.                     status == BluetoothHealth.APP_CONFIG_UNREGISTRATION_SUCCESS ?
  170.                     RESULT_OK : RESULT_FAIL);
  171.         }
  172.     }
  173.  
  174.     // Callback to handle channel connection state changes.
  175.     // Note that the logic of the state machine may need to be modified based on the HDP device.
  176.     // When the HDP device is connected, the received file descriptor is passed to the
  177.     // ReadThread to read the content.
  178.     public void onHealthChannelStateChange(BluetoothHealthAppConfiguration config,
  179.             BluetoothDevice device, int prevState, int newState, ParcelFileDescriptor fd,
  180.             int channelId) {
  181.         if (Log.isLoggable(TAG, Log.DEBUG))
  182.             Log.d(TAG, String.format("prevStatet%d ----------> newStatet%d",
  183.                     prevState, newState));
  184.         if (prevState == BluetoothHealth.STATE_CHANNEL_DISCONNECTED &&
  185.                 newState == BluetoothHealth.STATE_CHANNEL_CONNECTED) {
  186.             if (config.equals(mHealthAppConfig)) {
  187.                 mChannelId = channelId;
  188.                 sendMessage(STATUS_CREATE_CHANNEL, RESULT_OK);
  189.                 (new ReadThread(fd)).start();
  190.             } else {
  191.                 sendMessage(STATUS_CREATE_CHANNEL, RESULT_FAIL);
  192.             }
  193.         } else if (prevState == BluetoothHealth.STATE_CHANNEL_CONNECTING &&
  194.                    newState == BluetoothHealth.STATE_CHANNEL_DISCONNECTED) {
  195.             sendMessage(STATUS_CREATE_CHANNEL, RESULT_FAIL);
  196.         } else if (newState == BluetoothHealth.STATE_CHANNEL_DISCONNECTED) {
  197.             if (config.equals(mHealthAppConfig)) {
  198.                 sendMessage(STATUS_DESTROY_CHANNEL, RESULT_OK);
  199.             } else {
  200.                 sendMessage(STATUS_DESTROY_CHANNEL, RESULT_FAIL);
  201.             }
  202.         }
  203.     }
  204. };
  205.  
  206. // Sends an update message to registered UI client.
  207. private void sendMessage(int what, int value) {
  208.     if (mClient == null) {
  209.         Log.d(TAG, "No clients registered.");
  210.         return;
  211.     }
  212.  
  213.     try {
  214.         mClient.send(Message.obtain(null, what, value, 0));
  215.     } catch (RemoteException e) {
  216.         // Unable to reach client.
  217.         e.printStackTrace();
  218.     }
  219. }
  220.  
  221. // Thread to read incoming data received from the HDP device.  This sample application merely
  222. // reads the raw byte from the incoming file descriptor.  The data should be interpreted using
  223. // a health manager which implements the IEEE 11073-xxxxx specifications.
  224. private class ReadThread extends Thread {
  225.     private ParcelFileDescriptor mFd;
  226.  
  227.     public ReadThread(ParcelFileDescriptor fd) {
  228.         super();
  229.         mFd = fd;
  230.     }
  231.  
  232.     @Override
  233.     public void run() {
  234.         FileInputStream fis = new FileInputStream(mFd.getFileDescriptor());
  235.         final byte data[] = new byte[8192];
  236.         try {
  237.             while(fis.read(data) > -1) {
  238.                 // At this point, the application can pass the raw data to a parser that
  239.                 // has implemented the IEEE 11073-xxxxx specifications.  Instead, this sample
  240.                 // simply indicates that some data has been received.
  241.                 sendMessage(STATUS_READ_DATA, 0);
  242.             }
  243.         } catch(IOException ioe) {}
  244.         if (mFd != null) {
  245.             try {
  246.                 mFd.close();
  247.             } catch (IOException e) { /* Do nothing. */ }
  248.         }
  249.         sendMessage(STATUS_READ_DATA_DONE, 0);
  250.     }
  251. }