2014-09-23 4 views

답변

0

다음은 MetaWatch 프로그래밍의 예입니다. 블루투스 및 MetaWatch에 대한 소개.

하나의 명령을 보내고 하나의 응답을받습니다. onCreate()에서 호출 할 수 있습니다. (adb logcat에서)

static byte[] getDevTypeMessage = new byte[] { 
    0x01, 0x06, 0x01, 0x00, 0x0B, (byte) 0xD9 //The CRC is 0xD90B 
}; 
BluetoothSocket socket = null; 
void helloMetaWatch() { 
    BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); 
    if (mBluetoothAdapter == null) { 
     Log.e("~~~","no bluetooth"); 
    } else { 
     Log.e("~~~","found bluetooth"); 
     if (!mBluetoothAdapter.isEnabled()) { 
      Log.e("~~~","bluetooth not enabled"); 
     } else { 
      Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices(); 
      // If there are paired devices 
      if (pairedDevices.size() > 0) { 
       Log.d("~~~","paired devices: "+pairedDevices.size()); 
       // Loop through paired devices 
       for (BluetoothDevice device : pairedDevices) { 
        Log.d("~~~","device: ["+device+"] addr="+device.getAddress()+" name=["+device.getName()+"] type:"+getBTType(device)+" class:"+device.getBluetoothClass()); 
        if(device.getName().contains("MetaWatch")) { 
         BluetoothSocket temp = null; 
         try 
         { 
          //temp = btDevice.createRfcommSocketToServiceRecord(myUUID); 

          // calling an undocumented method: 
          Method m = device.getClass().getMethod("createRfcommSocket", new Class[] {int.class}); 
          temp = (BluetoothSocket) m.invoke(device, 1); 

         } //catch(IOException e) { } 
         catch (SecurityException e) { 
          e.printStackTrace(); 
         } catch (NoSuchMethodException e) { 
          e.printStackTrace(); 
         } catch (IllegalArgumentException e) { 
          e.printStackTrace(); 
         } catch (IllegalAccessException e) { 
          e.printStackTrace(); 
         } catch (InvocationTargetException e) { 
          e.printStackTrace(); 
         } 
         socket = temp; 
         Log.d("~~~","socket="+socket); 
         try { 
          socket.connect(); 
         } catch (IOException e) { 
          Log.e("~~~","~~~ could not connect:"); 
          e.printStackTrace(); 
         } 
         InputStream tmpIn = null; 
         OutputStream tmpOut = null; 

         try 
         { 
          tmpIn = socket.getInputStream(); 
          tmpOut = socket.getOutputStream(); 

          tmpOut.write(getDevTypeMessage); 

          // read response 
          long start = System.currentTimeMillis(); 
          while (System.currentTimeMillis() - start <= 1000) { 
           if (0 != tmpIn.available()) { 
            int readByte = tmpIn.read(); 
            Log.d("~~~","Read: " + readByte); 
           } else { 
            Log.d("~~~","waiting"); 
            try { 
             Thread.sleep(10,0); 
            } catch (InterruptedException e) { 
             e.printStackTrace(); 
            } 
           } 
          } 
          System.out.println("Complete in " + (System.currentTimeMillis() - start) + "ms"); 
         } 
         catch(IOException e) { 
          e.printStackTrace(); 
         } 

         try { 
          socket.close(); 
         } catch (IOException e) { 
          e.printStackTrace(); 
         } 
        } 
       } 
      } else { 
       Log.d("~~~","no paired devices"); 
      } 
     } 
    } 
} 
@SuppressLint("NewApi") 
String getBTType(BluetoothDevice device) { 
    try { 
     return ""+device.getType(); 
    } catch (Throwable x) { 
     return null; 
    } 
} 

출력은 같은 liiks :

D/~~~  (26862): waiting 
D/~~~  (26862): Read: 1 
D/~~~  (26862): Read: 7 
D/~~~  (26862): Read: 2 
D/~~~  (26862): Read: 0 
D/~~~  (26862): Read: 2 
D/~~~  (26862): Read: 95 
D/~~~  (26862): Read: 226 
D/~~~  (26862): waiting 
D/~~~  (26862): waiting 
D/~~~  (26862): waiting 

(. 7 개 라인이 읽은 후 결과가 89 사용할 수 있으며하기 전에, 당신이 타이밍을 추측 할 수있다 "대기") MetaWatch 연결을 받아 들일 준비가되지 않은 경우가 표시됩니다

E/~~~  (26736): ~~~ could not connect: 
W/System.err(26736): java.io.IOException: read failed, socket might closed or timeout, read ret: -1