蓝牙错误没有可通告的设备

2024-10-01 13:37:27 发布

您现在位置:Python中文网/ 问答频道 /正文

#! /usr/bin/python

import bluetooth
import uuid

server_socket = bluetooth.BluetoothSocket( bluetooth.RFCOMM )

port = 1
server_socket.bind(("",port))
server_socket.listen(1)

uuID = ##generated uuid

bluetooth.advertise_service( server_socket, "test", service_id=uuID )

client_socket, client_address = server_socket.accept()
print(client_socket)
print(client_address)

如果有人能帮忙,那就太好了。我已经试着把这里列出的说明看了5遍:Python code for Bluetooth throws error after I had to reset the adapter

我总是听到一个错误说bluetooth.btcommon.bluetooth错误:error no advertiable device“行号指向advertible\u服务行,无论我是否如pybluez github页面中的示例所示添加附加参数,还是将端口绑定到蓝牙端口 调用的方法如下:

^{pr2}$

如果我不做广告,我就无法打印出客户端信息,并且在android端得到一个空指针异常,所以我认为这是必要的,但是如果我做广告,就无法克服这个错误。 这是我能得到这个错误的最小代码量。正如我所提到的,不做广告不会出错,但我无法在connect上打印出客户端信息(android端找不到pi)。在

如果你知道一种不需要这部分的方法,下面是android代码:

    Set<BluetoothDevice> pairedDevices = BTAdapter.getBondedDevices();
            TextView textShowConnected = (TextView) findViewById(R.id.textShowConnected);
            if (pairedDevices.size() > 0) 
            {
                for (BluetoothDevice device : pairedDevices) 
                {
                    if(device.getName().toString().equals("Pi"))
                    {
                        textShowConnected.setText("Found the Pi.  Address is "+device.getAddress());
                        TextView textShowConnectedSocket = (TextView) findViewById(R.id.textShowConnectedSocket);
                        //textShowConnectedSocket.setText("uuid is: "+device.getUuids()[0].getUuid().toString());
                        try 
                        {
                            BluetoothSocket connection = device.createRfcommSocketToServiceRecord(device.getUuids()[0].getUuid());
                            //BluetoothSocket connection = device.createInsecureRfcommSocketToServiceRecord(device.getUuids()[0].getUuid());
                            connection.connect();
                            if(connection.isConnected())
                            {
                                textShowConnected.setText("Is connected from second.");
                                textShowConnectedSocket.setText("Is conencted to: "+connection.getRemoteDevice().getName().toString());
                                textShowPlace.setText("Is connected to: "+connection.getRemoteDevice().getAddress().toString());
                            }
                            else
                            {
                                textShowConnected.setText("No connection.");
                                textShowConnectedSocket.setText("No connection.");
                                textShowPlace.setText("No connection.");
                            }
                        } 
                        catch (IOException e) 
                        {
                            e.printStackTrace();
                        }
                    }
                    //DeviceItem newDevice = new DeviceItem(device.getName(), device.getAddress(), "false");
                    //deviceItemList.add(newDevice);
                }
            }
        }

我最近的Java端尝试(以防pi端没问题):

for (BluetoothDevice device : pairedDevices) 
{
    if(device.getName().toString().equals("Pi"))
    {
        textShowConnected.setText("Found the Pi.  Address is "+device.getAddress());
        TextView textShowConnectedSocket = (TextView) findViewById(R.id.textShowConnectedSocket);
        TextView textShowPlace = (TextView) findViewById(R.id.textShowPlace);
        //textShowConnectedSocket.setText("uuid is: "+device.getUuids()[0].getUuid().toString());
        int bt_port_to_connect = 1;
        BluetoothSocket deviceSocket = null;
        Method m = null;
        try {
            textShowPlace.setText("created socket");
            m = device.getClass().getMethod("createInsecureRfcommSocket", new Class[] {int.class});
            deviceSocket = (BluetoothSocket) m.invoke(device,bt_port_to_connect);
            deviceSocket.connect();
            if(deviceSocket.isConnected()) 
            {
                textShowPlace.setText("is connected");
                textShowConnectedSocket.setText("Connected successfully.");
                textShowConnected.setText("Connected to: "+deviceSocket.getRemoteDevice().getName().toString());
            }
            else
            {
                textShowConnectedSocket.setText("Did not connect.");
            }
        } 
        catch (IOException e) 
        {
            textShowPlace.setText("catch statement "+e);
            textShowConnectedSocket.setText("No connection.");
        } 
        catch (NoSuchMethodException e) 
        {
            textShowConnected.setText("No such method.");
            textShowPlace.setText("catch statement "+e);
        } 
        catch (InvocationTargetException e) 
        {
            textShowPlace.setText("catch statement "+e);
            textShowConnectedSocket.setText("No connection.");
        } 
        catch (IllegalAccessException e) 
        {
            textShowPlace.setText("catch statement "+e);
            textShowConnectedSocket.setText("No connection.");
        }
        //device.createRfcommSocketToServiceRecord(uuid);                                                                            
        //device.createInsecureRfcommSocketToServiceRecord(uuid);
    }
    //DeviceItem newDevice = new DeviceItem(device.getName(), device.getAddress(), "false");
    //deviceItemList.add(newDevice);
}

如果你能帮我接通连接,我会很感激的。我不知道这是怎么回事。在


Tags: tonouuiddeviceconnectsocketconnectionbluetooth
1条回答
网友
1楼 · 发布于 2024-10-01 13:37:27

看起来像是在做广告只是需要:“sudo hciconfig hci0 piscan”

并将其连接起来蓝牙端口从那个进口,所以我把这个标记为已回答。在

任何人发现这是因为他们自己的问题。你现在有答案了。祝你好运。在

相关问题 更多 >