2012-06-12 60 views
1

我正在編寫一個需要在Android 2.0中運行的程序。我目前正試圖將我的android設備連接到嵌入式藍牙芯片。我已經提供了有關使用fetchuidsWithSDP()或getUuids()的信息,但是我閱讀的頁面解釋了這些方法隱藏在2.0 SDK中,並且必須使用反射來調用。我不知道這意味着什麼,也沒有解釋。給出了一些示例代碼,但很少有解釋。我希望有人能夠幫助我理解這裏實際發生的事情,因爲我對Android開發非常陌生。在Android 2.0中查找UUID

String action = "android.bleutooth.device.action.UUID"; 
IntentFilter filter = new IntentFilter(action); 
registerReceiver(mReceiver, filter); 

我讀的頁面還說,在第一行中,藍牙是故意拼寫爲「bleutooth」。如果任何人都可以解釋這一點,我將不勝感激,除非開發者犯了錯誤,否則對我來說毫無意義。

private final BroadcastReceiver mReceiver = new BroadcastReceiver() { 
@Override 
public void onReceive(Context context, Intent intent) { 
    BluetoothDevice deviceExtra = intent.getParcelableExtra("android.bluetooth.device.extra.Device"); 
    Parcelable[] uuidExtra = intent.getParcelableArrayExtra("android.bluetooth.device.extra.UUID"); 
} 

};

我很難理解我爲我的嵌入式藍牙芯片找到了正確的UUID。如果任何人可以幫助它將不勝感激。

編輯:我要添加我的onCreate()方法的其餘部分,以便您可以看到我正在處理的內容。

public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    // Set up window View 
    setContentView(R.layout.main); 

    // Initialize the button to scan for other devices. 
    btnScanDevice = (Button) findViewById(R.id.scandevice); 

    // Initialize the TextView which displays the current state of the bluetooth 
    stateBluetooth = (TextView) findViewById(R.id.bluetoothstate); 
    startBluetooth(); 

    // Initialize the ListView of the nearby bluetooth devices which are found. 
    listDevicesFound = (ListView) findViewById(R.id.devicesfound); 
    btArrayAdapter = new ArrayAdapter<String>(AndroidBluetooth.this, 
      android.R.layout.simple_list_item_1); 
    listDevicesFound.setAdapter(btArrayAdapter); 

    CheckBlueToothState(); 

    // Add an OnClickListener to the scan button. 
    btnScanDevice.setOnClickListener(btnScanDeviceOnClickListener); 

    // Register an ActionFound Receiver to the bluetooth device for ACTION_FOUND 
    registerReceiver(ActionFoundReceiver, new IntentFilter(BluetoothDevice.ACTION_FOUND)); 

    // Add an item click listener to the ListView 
    listDevicesFound.setOnItemClickListener(new OnItemClickListener() 
    { 
     public void onItemClick(AdapterView<?> arg0, View arg1,int arg2, long arg3) 
     { 
      // Save the device the user chose. 
      myBtDevice = btDevicesFound.get(arg2); 

      // Open a socket to connect to the device chosen. 
      try { 
       btSocket = myBtDevice.createRfcommSocketToServiceRecord(MY_UUID); 
      } catch (IOException e) { 
       Log.e("Bluetooth Socket", "Bluetooth not available, or insufficient permissions"); 
      } catch (NullPointerException e) { 
       Log.e("Bluetooth Socket", "Null Pointer One"); 
      } 

      // Cancel the discovery process to save battery. 
      myBtAdapter.cancelDiscovery(); 

      // Update the current state of the Bluetooth. 
      CheckBlueToothState(); 

      // Attempt to connect the socket to the bluetooth device. 
      try { 
       btSocket.connect();     
       // Open I/O streams so the device can send/receive data. 
       iStream = btSocket.getInputStream(); 
       oStream = btSocket.getOutputStream(); 
      } catch (IOException e) { 
       Log.e("Bluetooth Socket", "IO Exception"); 
      } catch (NullPointerException e) { 
       Log.e("Bluetooth Socket", "Null Pointer Two"); 
      } 
     } 
    }); 
} 
+1

你可能會給該網頁的鏈接? –

+0

是的,它就在這裏 http://wiresareobsolete.com/wordpress/2010/11/android-bluetooth-rfcomm/ – JuiCe

回答

5

你可能會更好過使用同步的版本,所以你不必應付建立BroadcastReceiver的所有運動部件。由於您總是在發現之後執行此操作,因此緩存的數據將始終保持新鮮。

這裏將UUID數據封裝到一個方法中的功能。此代碼是你鏈接的博客文章的評論之一:

//In SDK15 (4.0.3) this method is now public as 
//Bluetooth.fetchUuisWithSdp() and BluetoothDevice.getUuids() 
public ParcelUuid[] servicesFromDevice(BluetoothDevice device) { 
    try { 
     Class cl = Class.forName("android.bluetooth.BluetoothDevice"); 
     Class[] par = {}; 
     Method method = cl.getMethod("getUuids", par); 
     Object[] args = {}; 
     ParcelUuid[] retval = (ParcelUuid[]) method.invoke(device, args); 
     return retval; 
    } catch (Exception e) { 
     e.printStackTrace(); 
     return null; 
    } 
} 

然後你可以在你的代碼的任何地方調用此方法,傳遞一個BluetoothDevice並取回的UUID的數組,該設備提供的服務(通常對於小型嵌入式堆棧,數組只有1個項目);例如:

// Save the device the user chose. 
    myBtDevice = btDevicesFound.get(arg2); 
    //Query the device's services 
    ParcelUuid[] uuids = servicesFromDevice(myBtDevice); 

    // Open a socket to connect to the device chosen. 
    try { 
     btSocket = myBtDevice.createRfcommSocketToServiceRecord(uuids[0].getUuid()); 
    } catch (IOException e) { 
     Log.e("Bluetooth Socket", "Bluetooth not available, or insufficient permissions"); 
    } catch (NullPointerException e) { 
     Log.e("Bluetooth Socket", "Null Pointer One"); 
    } 

您在上面張貼的區塊中。

作爲一個方面說明,以您所使用的方式調用所有這些代碼將會讓您的應用程序在以後變得很難受。調用connect()並獲取流的代碼塊應在後臺線程上完成,因爲該方法會阻塞一段時間,並且在主線程上調用此代碼會暫時凍結您的UI。您應該將該代碼移入AsyncTaskThread,就像SDK中的BluetoothChat示例一樣。

HTH

+0

你是老闆非常感謝,非常感謝 – JuiCe

+0

如果你碰巧看看這個,我今天再次回到這個代碼,並意識到它仍然不適合我。如果您有機會提供幫助,我發佈了一個新問題。 http://stackoverflow.com/questions/11105891/how-do-i-find-the-proper-uuid – JuiCe

+1

@Devunwired你好我喜歡ParcelUuid [] retval爲null時,寧願上面的代碼。你能幫忙嗎? –

-1

我也面臨着同樣的問題,這是我如何解決它爲Android 2.3.3。我認爲相同的解決方案也適用於android 2.2。

private final BroadcastReceiver mReceiver = new BroadcastReceiver() { 
    @SuppressLint("NewApi") 
    @Override 
    public void onReceive(Context context, Intent intent) { 
     String action = intent.getAction(); 

     // When discovery finds a device 
     if (BluetoothDevice.ACTION_FOUND.equals(action)) { 
      // Get the BluetoothDevice object from the Intent 
      BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); 
      Toast.makeText(getApplicationContext(),"Device: "+device.getName(),Toast.LENGTH_SHORT).show(); 
      devices.add(device.getName() + "\n" + device.getAddress()); 
      list.add(device); 

     } 
     else if(BluetoothDevice.ACTION_UUID.equals(action)){ 
      Toast.makeText(getApplicationContext(),"I am Here",Toast.LENGTH_SHORT).show(); 
     } 
     else { 
      if(BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) { 
       Toast.makeText(getApplicationContext(),"Done Scanning..",Toast.LENGTH_SHORT).show(); 
       Iterator<BluetoothDevice> itr = list.iterator(); 
       while(itr.hasNext()) 
       { 
        BluetoothDevice dev=itr.next(); 
        if(dev.fetchUuidsWithSdp()) 
        { 
         Parcelable a[]=dev.getUuids(); 
         Toast.makeText(getApplicationContext(),dev.getName()+":"+a[0],Toast.LENGTH_SHORT).show(); 
        } 
       } 
      } 
     }  
    } 
};