Android 我的蓝牙无法连接到设备

Android 我的蓝牙无法连接到设备,android,android-studio,bluetooth,Android,Android Studio,Bluetooth,我正在尝试使用我的android手机连接到pc或hc01,但我知道我编写的代码现在无法连接到手机,因此任何人都可以帮助我 UUID我应该用什么,或者让任何人用一些恶魔让我试试 谢谢 我不知道为什么我点击它,它总是连接失败,而logcat会 com.example.lord1.myapplication123123 W/BluetoothAdapter: getBluetoothService() called with no BluetoothManagerCallback 这是我的代码: p

我正在尝试使用我的android手机连接到pc或hc01,但我知道我编写的代码现在无法连接到手机,因此任何人都可以帮助我 UUID我应该用什么,或者让任何人用一些恶魔让我试试 谢谢

我不知道为什么我点击它,它总是连接失败,而logcat会

com.example.lord1.myapplication123123 W/BluetoothAdapter: getBluetoothService() called with no BluetoothManagerCallback
这是我的代码:

private void discover(View view){
    // Check if the device is already discovering
    if(mBTAdapter.isDiscovering()){
        mBTAdapter.cancelDiscovery();
        Toast.makeText(getApplicationContext(),"Discovery stopped",Toast.LENGTH_SHORT).show();
    }
    else{
        if(mBTAdapter.isEnabled()) {
            mBTArrayAdapter.clear(); // clear items
            mBTAdapter.startDiscovery();
            Toast.makeText(getApplicationContext(), "Discovery started", Toast.LENGTH_SHORT).show();
            registerReceiver(blReceiver, new IntentFilter(BluetoothDevice.ACTION_FOUND));
        }
        else{
            Toast.makeText(getApplicationContext(), "Bluetooth not on", Toast.LENGTH_SHORT).show();
        }
    }
}

final BroadcastReceiver blReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        if(BluetoothDevice.ACTION_FOUND.equals(action)){
            BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
            // add the name to the list
            mBTArrayAdapter.add(device.getName() + "\n" + device.getAddress());
            mBTArrayAdapter.notifyDataSetChanged();

        }

    }
};

private void listPairedDevices(View view){
    mPairedDevices = mBTAdapter.getBondedDevices();
    if(mBTAdapter.isEnabled()) {
        // put it's one to the adapter
        for (BluetoothDevice device : mPairedDevices)
            mBTArrayAdapter.add(device.getName() + "\n" + device.getAddress());

        Toast.makeText(getApplicationContext(), "Show Paired Devices", Toast.LENGTH_SHORT).show();
    }
    else
        Toast.makeText(getApplicationContext(), "Bluetooth not on", Toast.LENGTH_SHORT).show();
}

private AdapterView.OnItemClickListener mDeviceClickListener = new AdapterView.OnItemClickListener() {
    public void onItemClick(AdapterView<?> av, View v, int arg2, long arg3) {

        if(!mBTAdapter.isEnabled()) {
            Toast.makeText(getBaseContext(), "Bluetooth not on", Toast.LENGTH_SHORT).show();
            return;
        }

        mBluetoothStatus.setText("Connecting...");
        // Get the device MAC address, which is the last 17 chars in the View
        String info = ((TextView) v).getText().toString();
        final String address = info.substring(info.length() - 17);
        final String name = info.substring(0,info.length() - 17);

        // Spawn a new thread to avoid blocking the GUI one
        new Thread()
        {
            public void run() {
                boolean fail = false;

                BluetoothDevice device = mBTAdapter.getRemoteDevice(address);

                try {
                    mBTSocket = createBluetoothSocket(device);
                } catch (IOException e) {
                    fail = true;
                    Toast.makeText(getBaseContext(), "Socket creation failed", Toast.LENGTH_SHORT).show();
                }
                // Establish the Bluetooth socket connection.
                try {
                    mBTSocket.connect();
                } catch (IOException e) {
                    try {
                        fail = true;
                        mBTSocket.close();
                        mHandler.obtainMessage(CONNECTING_STATUS, -1, -1)
                                .sendToTarget();
                    } catch (IOException e2) {
                        //insert code to deal with this
                        Toast.makeText(getBaseContext(), "Socket creation failed", Toast.LENGTH_SHORT).show();
                    }
                }
                if(fail == false) {
                    mConnectedThread = new ConnectedThread(mBTSocket);
                    mConnectedThread.start();

                    mHandler.obtainMessage(CONNECTING_STATUS, 1, -1, name)
                            .sendToTarget();
                }
            }
        }.start();
    }
};
private void discover(查看){
//检查设备是否已发现
if(mBTAdapter.isDiscovering()){
mBTAdapter.cancelDiscovery();
Toast.makeText(getApplicationContext(),“发现已停止”,Toast.LENGTH\u SHORT.show();
}
否则{
if(mBTAdapter.isEnabled()){
mBTArrayAdapter.clear();//清除项
mBTAdapter.startDiscovery();
Toast.makeText(getApplicationContext(),“发现已开始”,Toast.LENGTH\u SHORT.show();
registerReceiver(blReceiver,新的IntentFilter(BluetoothDevice.ACTION_-FOUND));
}
否则{
Toast.makeText(getApplicationContext(),“蓝牙未打开”,Toast.LENGTH_SHORT.show();
}
}
}
final BroadcastReceiver blReceiver=新的BroadcastReceiver(){
@凌驾
公共void onReceive(上下文、意图){
String action=intent.getAction();
if(BluetoothDevice.ACTION_FOUND.equals(ACTION)){
BluetoothDevice=intent.getParcelableExtra(BluetoothDevice.EXTRA\u设备);
//将名称添加到列表中
mBTArrayAdapter.add(device.getName()+“\n”+device.getAddress());
mBTArrayAdapter.notifyDataSetChanged();
}
}
};
私有void listPairedDevices(视图){
mPairedDevices=mBTAdapter.getBondedDevices();
if(mBTAdapter.isEnabled()){
//把它放在适配器上
用于(蓝牙设备:mPairedDevices)
mBTArrayAdapter.add(device.getName()+“\n”+device.getAddress());
Toast.makeText(getApplicationContext(),“显示配对设备”,Toast.LENGTH_SHORT.Show();
}
其他的
Toast.makeText(getApplicationContext(),“蓝牙未打开”,Toast.LENGTH_SHORT.show();
}
私有AdapterView.OnItemClickListener mDeviceClickListener=新AdapterView.OnItemClickListener(){
公共链接(AdapterView av、视图v、内部arg2、长arg3){
如果(!mBTAdapter.isEnabled()){
Toast.makeText(getBaseContext(),“蓝牙未打开”,Toast.LENGTH_SHORT.show();
返回;
}
mBluetoothStatus.setText(“连接…”);
//获取设备MAC地址,这是视图中的最后17个字符
字符串信息=((TextView)v.getText().toString();
最终字符串地址=信息子字符串(信息长度()-17);
最后一个字符串名称=info.substring(0,info.length()-17);
//生成一个新线程以避免阻塞GUI线程
新线程()
{
公开募捐{
布尔失败=错误;
BluetoothDevice=mBTAdapter.getRemoteDevice(地址);
试一试{
mBTSocket=createBluetoothSocket(设备);
}捕获(IOE异常){
失败=正确;
Toast.makeText(getBaseContext(),“套接字创建失败”,Toast.LENGTH_SHORT.show();
}
//建立蓝牙套接字连接。
试一试{
mBTSocket.connect();
}捕获(IOE异常){
试一试{
失败=正确;
mBTSocket.close();
mHandler.ActainMessage(连接状态,-1,-1)
.sendToTarget();
}捕获(IOE2异常){
//插入处理此问题的代码
Toast.makeText(getBaseContext(),“套接字创建失败”,Toast.LENGTH_SHORT.show();
}
}
if(fail==false){
mConnectedThread=新连接的线程(mBTSocket);
mConnectedThread.start();
mHandler.ActainMessage(连接状态,1,-1,名称)
.sendToTarget();
}
}
}.start();
}
};

请从单击事件开始发布日志。并在您的代码中添加
日志
,而不是
祝酒
,这样更容易遵循
日志
。我不知道如何使用Transform服务此代码是将蓝牙写入smae页面,如果我更改页面,蓝牙将关闭蓝牙