近くのすべてのBluetoothデバイスを検出して接続するアプリを作成する必要があります。私はおそらく、Javaで動作するこのコードを持っていますが、私はそれがC#で作業する必要があります。近くのBluetoothデバイスをAndroidで取得する#
public class BroadcastReciverClass : BroadcastReceiver
{
public override void OnReceive(Context context, Intent intent)
{
String action = intent.Action;
// When discovery finds a device
if (BluetoothDevice.ActionFound.Equals(action))
{
BluetoothDevice device = intent.GetParcelableExtra(BluetoothDevice.ExtraDevice);
// Add the name and address to an array adapter to show in a ListView
mArrayAdapter.Add(device.Name+" -> "+device.Address);
lv.Adapter = mArrayAdapter;
}
}
}
問題は、Visual Studioが言うことです:
は、暗黙的にjava.lang.Object上位を変換できません。 Android.Bluetooth.BluetoothDeviceに。
キャストがありませんか?この行の
:
BluetoothDevice device = intent.GetParcelableExtra(BluetoothDevice.ExtraDevice);
ここで、デバイスがランタイムでJava.NullPointerExceptionを検出した場合 – KillGeek
@KillGeek 'null'値をテストしますか?また、これらの例では 'device'インスタンスは' if(BluetoothDevice.ActionFound.Equals(action)) 'ブロックの中で宣言されています。その 'if'ブロックの外で使用したい場合は、' if'ブロックの外で宣言する必要があります。 –
ありがとうございました – KillGeek