gpt4 book ai didi

java - 将蓝牙的 Mac 地址传递给其他 Activity

转载 作者:行者123 更新时间:2023-12-01 10:44:07 25 4
gpt4 key购买 nike

debug image here 蓝牙Activity.Java [调试图像2][2]

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.bluetooth_main);
//myLabel = (TextView)findViewById(R.id.label);
toggleButton = (ToggleButton) findViewById(R.id.toggleButton);
listview = (ListView) findViewById(R.id.listView);
// ListView Item Click Listener
listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// ListView Clicked item value
String itemValue = (String) listview.getItemAtPosition(position);
Toast.makeText(getApplicationContext(), "clicked", Toast.LENGTH_LONG).show();
String MAC = itemValue.substring(itemValue.length() - 17);
Intent intent = new Intent(getApplicationContext(),Bluetooth_dataDisplay.class);
intent.putExtra("MAC",MAC);
startActivity(intent);
//BluetoothDevice bluetoothDevice = mBluetoothAdapter.getRemoteDevice(MAC);
// Initiate a connection request in a separate thread
//ConnectingThread t = new ConnectingThread(bluetoothDevice);
//t.start();
}
});

adapter = new ArrayAdapter
(this,android.R.layout.simple_list_item_1);
listview.setAdapter(adapter);

//verify bluetooth is supported on the device
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
// Device does not support Bluetooth
if (mBluetoothAdapter == null) {
}

}//end oncreate

Bluetooth_datadisplay.Java bluetooth_datadisplay image

 @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.bluetooth_datadisplay);

myLabel = (TextView)findViewById(R.id.label);

String MAC = getIntent().getStringExtra("MAC");

BluetoothDevice bluetoothDevice = mAdapter.getRemoteDevice(MAC);

// Initiate a connection request in a separate thread
ConnectingThread t = new ConnectingThread(bluetoothDevice);

t.start();

}//end oncreate

错误

尝试在空对象引用上调用虚拟方法“android.bluetooth.BluetoothDevice android.bluetooth.BluetoothAdapter.getRemoteDevice(java.lang.String)

您好,我尝试将蓝牙的 Mac 地址传递给蓝牙数据显示器。但我得到了上面的错误。谁能向我解释一下出了什么问题吗?谢谢你!

最佳答案

尚未定义mAdapter的对象

BluetoothAdapter mAdapter = BluetoothAdapter.getDefaultAdapter();    
BluetoothDevice bluetoothDevice = mAdapter.getRemoteDevice(MAC);

希望这对您有帮助。

关于java - 将蓝牙的 Mac 地址传递给其他 Activity ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34290243/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com