我正在学习android并正在研究使用蓝牙的项目应用程序。我已经能够发现并列出设备。但是,当我尝试配对设备时,socket.connect()每次都会失败。我是Java和android的新手。
我尝试了不安全的连接,默认的UUID和设备的UUID,但仍然得到了相同的结果和日志。
这是到目前为止的完整代码
package com.example.bluetoothhc;
import android.bluetooth.BluetoothSocket;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.Manifest;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.support.v4.app.ActivityCompat;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Set;
import java.util.UUID;
import static com.example.bluetoothhc.R.id.listView;
public class MainActivity extends AppCompatActivity {
private BluetoothSocket socket;
private BluetoothDevice mmDevice;
private UUID DEFAULT_UUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
ListView listView;
TextView statusView;
Button searchButton;
ArrayList<String> bluetoothDevices = new ArrayList<>();
ArrayAdapter arrayAdapter;
//defining a common tag
private final static String TAG = "Main Activity";
//BT adapter is one for the entire system so define it globally
private BluetoothAdapter bluetoothAdaptera;
private final BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
Log.i("Action2",action);
if(BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)){
statusView.setText("Finished");
searchButton.setEnabled(true);
Log.i("Action3","df");
}
else if(BluetoothDevice.ACTION_FOUND.equals(action)){
Log.i("Action34","dfsff");
// Discovery has found a device. Get the BluetoothDevice
// object and its info from the Intent.
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
String name = device.getName();
String Address = device.getAddress(); // MAC address
//distance between deivce or signal strength, more negative favourable
//String rssi = Integer.toString(intent.getShortExtra(BluetoothDevice.EXTRA_RSSI,Short.MIN_VALUE));
Log.i("device found","Name: "+ name +"Address: " + Address );
// String deviceInfo = "";
// if(name== null || name.equals("")){
// deviceInfo += Address;
// }
// else{
// deviceInfo += name ;
// }
String deviceInfo=Address;
if(!bluetoothDevices.contains(deviceInfo)) {
bluetoothDevices.add(deviceInfo);
}
arrayAdapter.notifyDataSetChanged();
}
}
};
public void SearchClicked(View view){
arrayAdapter.clear();
statusView.setText("Searching...");
searchButton.setEnabled(false);
Log.i("info - searchClicked","search button Disabled");
int MY_PERMISSIONS_REQUEST_ACCESS_COARSE_LOCATION = 1;
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.ACCESS_COARSE_LOCATION},
MY_PERMISSIONS_REQUEST_ACCESS_COARSE_LOCATION);
if(bluetoothAdaptera.isDiscovering()){
bluetoothAdaptera.cancelDiscovery();
Log.i("SearchCliced","Canceling discovery");
}
Log.i("SearchCliced","Starting Discovering");
bluetoothAdaptera.startDiscovery();
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//assing UI Components
listView = findViewById(R.id.listView);
statusView = findViewById(R.id.statusTextView);
searchButton = findViewById(R.id.SearchButtonView);
arrayAdapter = new ArrayAdapter(this,android.R.layout.simple_list_item_1,bluetoothDevices);
listView.setAdapter(arrayAdapter);
//getting the BT adapter
bluetoothAdaptera = BluetoothAdapter.getDefaultAdapter();
// check if bluetooth adapter is present in the device
if(bluetoothAdaptera == null){
Toast.makeText(getApplicationContext(),"Device doesn't support Bluetooth", Toast.LENGTH_LONG).show();
finish();
return;
}
int REQUEST_ENABLE_BT = 1;
//check if BT is enabled
if(!bluetoothAdaptera.isEnabled()){
Intent enableBTIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBTIntent, REQUEST_ENABLE_BT);
}
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(BluetoothAdapter.ACTION_STATE_CHANGED);
intentFilter.addAction(BluetoothDevice.ACTION_FOUND);
intentFilter.addAction(BluetoothAdapter.ACTION_DISCOVERY_STARTED);
intentFilter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
intentFilter.addAction(BluetoothDevice.EXTRA_DEVICE);
intentFilter.addAction(BluetoothDevice.ACTION_UUID);
intentFilter.addAction(BluetoothDevice.ACTION_BOND_STATE_CHANGED);
registerReceiver(broadcastReceiver, intentFilter);
// WE NEED TO CREATE ANOTHER LIST FOR PAIRED DEVICES FOR ONLCIK EVENTS FUNCTION
Set<BluetoothDevice> pairedDevices = bluetoothAdaptera.getBondedDevices();
if (pairedDevices.size() > 0) {
// There are paired devices. Get the name and address of each paired device.
for (BluetoothDevice device : pairedDevices) {
String deviceName = device.getName();
String deviceHardwareAddress = device.getAddress(); // MAC address
Log.i("device",""+deviceName +" "+deviceHardwareAddress);
if(deviceName== null || deviceName.equals("")){
bluetoothDevices.add(deviceHardwareAddress );
}
else{
bluetoothDevices.add(deviceHardwareAddress );
// bluetoothDevices.add(deviceName );
}
}
}
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Log.i("clickList1","itemclicked");
String address = bluetoothDevices.get(position);
BluetoothDevice device = bluetoothAdaptera.getRemoteDevice(address);
Log.i("BTDevice C Name", device.getAddress());
Log.d(TAG, address);
BluetoothSocket tmp=null;
bluetoothAdaptera.cancelDiscovery();
mmDevice = device;
try {
// Use the UUID of the device that discovered //
if (mmDevice != null)
{
Log.i(TAG, "Device Name1: " + mmDevice.getName());
Log.i(TAG, "Device UUID1: " + mmDevice.getUuids()[0].getUuid());
tmp = device.createRfcommSocketToServiceRecord( mmDevice.getUuids()[0].getUuid());
// createMethod = device.getClass().getMethod("createInsecureRfcommSocket", new Class[] { int.class });
// tmp = (BluetoothSocket)createMethod.invoke(device, 1);
// tmp = device.createInsecureRfcommSocketToServiceRecord(mmDevice.getUuids()[0].getUuid());
Log.i("DEvice UUID","socket created successfully with device uuid");
}
else Log.d(TAG, "Device is null.");
}
catch (NullPointerException e)
{
Log.d(TAG, " UUID from device is null, Using Default UUID, Device name: " + device.getName());
try {
tmp = device.createRfcommSocketToServiceRecord(DEFAULT_UUID);
Log.i("default UUID","socket created successfully with default uuid");
} catch (IOException e1) {
e1.printStackTrace();
Log.i("IOEX","printStack");
}
}
catch (IOException e) {
Log.i("IOException","2");
}
socket = tmp;
try {
Log.i(TAG,"Trying to connect");
// Connect to the remote device through the socket. This call blocks
// until it succeeds or throws an exception.
socket.connect();
Log.i(TAG,"connection_successful");
} catch (IOException connectException) {
// Unable to connect; close the socket and return.
try {
socket.close();
Log.i(TAG,"connection failed - socket closed");
} catch (IOException closeException) {
Log.e(TAG, "Could not close the client socket", closeException);
}
return;
}
}
});
}
@Override
protected void onDestroy() {
super.onDestroy();
// Don't forget to unregister the ACTION_FOUND receiver.
unregisterReceiver(broadcastReceiver);
}
}
答案 0 :(得分:0)
首先,您必须使用BluetoothAdapter使用此方法listenUsingRfcommWithServiceRecord()创建BluetoothServerSocket:
BluetoothServerSocket mServerSocket = mBluetoothAdapter.listenUsingRfcommWithServiceRecord(NAME, MY_UUID);
然后您可以创建
BluetoothSocket socket = mServerSocket.accept();
然后使用
socket.connect();
使用此方法连接后,您必须执行以下操作:
BluetoothDevice device = socket.getRemoteDevice();
BluetoothSocket tmp = device.createRfcommSocketToServiceRecord(DEVICE_UUID);
tmp.connect();
因此,让我清除一下,您正在直接使用createRfcommSocketToServiceRecord()方法并尝试连接套接字,而不是尝试这种方式。我希望它现在可以消除您的疑虑。
但是请记住,listenUsingRfcommWithServiceRecord()是一个很长的后台处理方法,因此在执行线程或异步任务时要小心。希望能帮助到你。谢谢!