gpt4 book ai didi

android - 尽管尝试了教程,但蓝牙设备未连接到 Android 应用程序

转载 作者:行者123 更新时间:2023-12-03 16:09:03 25 4
gpt4 key购买 nike

我不知道它有什么问题,但我的应用程序正在搜索附近的蓝牙设备,但是当我尝试连接到这些蓝牙设备之一时,应用程序崩溃了。

请看一看

这是我的搜索BTDevice.java .这是调用 ConnectBTDevice Activity 的 Activity

package vertex2016.mvjce.edu.bluealert;

import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothGattDescriptor;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.bluetooth.BluetoothAdapter;
import android.provider.Settings;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

import java.util.Set;

public class SearchBTDevice extends AppCompatActivity {

public BluetoothAdapter BlueAdapter = BluetoothAdapter.getDefaultAdapter();
public ArrayAdapter PairedArrayAdapter;
public ArrayAdapter BTArrayAdapter;
BluetoothDevice btd;

public ListView devicesFound;


private final BroadcastReceiver BTReceiver= new BroadcastReceiver(){

public void onReceive(Context context, Intent intent)
{
String action = intent.getAction();

if (BluetoothDevice.ACTION_FOUND.equals(action)) {
btd = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);

BTArrayAdapter.add(btd.getName() + "\t" + btd.getAddress() + "\n");

}
}

};

IntentFilter filter1 = new IntentFilter(BluetoothDevice.ACTION_FOUND);




@Override
protected void onResume() {
super.onResume();
this.registerReceiver(BTReceiver,filter1);



}

@Override
protected void onPause() {
super.onPause();
BlueAdapter.cancelDiscovery();
this.unregisterReceiver(BTReceiver);
Toast.makeText(SearchBTDevice.this, "Discovery Stopped!!", Toast.LENGTH_SHORT).show();
}


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_search_btdevice);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LOCKED);




searchBTDevices();


}


public void searchBTDevices()
{
if(!BlueAdapter.startDiscovery())
Toast.makeText(SearchBTDevice.this, "Failed to Start Discovery", Toast.LENGTH_SHORT).show();
else
Toast.makeText(SearchBTDevice.this, "Discovery Startred", Toast.LENGTH_SHORT).show();
BTArrayAdapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1);
devicesFound = (ListView)findViewById(R.id.searchpagelistView);
devicesFound.setAdapter(BTArrayAdapter);
devicesFound.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent connectedBTintent = new Intent(SearchBTDevice.this, ConnectedBTDevice.class);
connectedBTintent.putExtra("BluetoothDevice", btd);
startActivity(connectedBTintent);

}
});

}


}

这是我的 更新 ConnectBTDevice.java 试图建立RFCOMM channel 。但这是我的应用程序崩溃的地方。
    package vertex2016.mvjce.edu.bluealert;

import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import java.io.IOException;
import java.util.UUID;

public class ConnectedBTDevice extends AppCompatActivity {

public BluetoothDevice btd;
public BluetoothSocket btSocket, tempSocket;
private UUID myUUID;
ArrayAdapter arr;
ListView lv;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_connected_btdevice);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LOCKED);


arr = new ArrayAdapter(this, android.R.layout.simple_list_item_2);

btd = getIntent().getParcelableExtra("BluetoothDevice");

connectBT();
displayStuff();

}

public void connectBT() {
Thread myThread = new Thread() {

public void run() {
tempSocket = null;

try {
tempSocket = btd.createRfcommSocketToServiceRecord(myUUID);
} catch (IOException e) {
e.printStackTrace();
}

BluetoothAdapter.getDefaultAdapter().cancelDiscovery();

try {
btSocket.connect();
arr.add("CONNECTED TO-->" + btd.getName());
} catch (IOException e) {
e.printStackTrace();
try {
btSocket.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}

};
myThread.start();
}




public void displayStuff()
{
lv = (ListView)findViewById(R.id.connectedBTlistView);
lv.setAdapter(arr);
}

}

这就是我的 更新 日志猫 节目
03-22 01:40:47.116 28214-11199/vertex2016.mvjce.edu.bluealert E/AndroidRuntime: FATAL EXCEPTION: Thread-11810
Process: vertex2016.mvjce.edu.bluealert, PID: 28214
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.bluetooth.BluetoothSocket.connect()' on a null object reference
at vertex2016.mvjce.edu.bluealert.ConnectedBTDevice$1.run(ConnectedBTDevice.java:61)

最佳答案

logcat 中的异常告诉您问题所在。您正在尝试调用 BluetoothSocket.connect()在空对象引用上。

您调用btSocket.connect()但 btSocket 从未被初始化,因此它为空。

调用tempSocket.connect()或分配 tempSocketbtSocket在你面前connect() .

关于android - 尽管尝试了教程,但蓝牙设备未连接到 Android 应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36098231/

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