我正在编写一个需要在 Android 2.0 中运行的程序。我目前正在尝试将我的 android 设备连接到嵌入式蓝牙芯片。我已获得有关使用 fetchuidsWithSDP() 或 getUuids() 的信息,但我阅读的页面解释说这些方法隐藏在 2.0 SDK 中,必须使用反射调用。我不知道那是什么意思,也没有解释。给出了示例代码,但背后的解释很少。我希望有人可以帮助我了解这里实际发生了什么,因为我对 Android 开发非常陌生。
String action = "android.bleutooth.device.action.UUID";
IntentFilter filter = new IntentFilter( action );
registerReceiver( mReceiver, filter );
我阅读的页面还说,在第一行中,蓝牙故意拼写为“蓝牙”。如果有人能解释这一点,我将不胜感激,除非开发人员打错了字,否则这对我来说毫无意义。
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" );
}
}
});
}