我正在編寫一個需要在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");
}
}
});
}
你可能會給該網頁的鏈接? –
是的,它就在這裏 http://wiresareobsolete.com/wordpress/2010/11/android-bluetooth-rfcomm/ – JuiCe