#! /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)
If anyone could help with this, that would be great. I've tried going through the instructions listed here about 5 times: Python code for Bluetooth throws error after I had to reset the adapter
I keep getting an error saying "bluetooth.btcommon.BluetoothError: error no advertisable device" The line number points to the advertise_service line, and it does so whether or not I add the additional parameters as shown in the example in the pybluez github page, or bind the port to bluetooth.PORT_ANY The method being called is here:
def advertise_service (sock, name, service_id = "", service_classes = [], \
profiles = [], provider = "", description = "", protocols = []):
if service_id != "" and not is_valid_uuid (service_id):
raise ValueError ("invalid UUID specified for service_id")
for uuid in service_classes:
if not is_valid_uuid (uuid):
raise ValueError ("invalid UUID specified in service_classes")
for uuid, version in profiles:
if not is_valid_uuid (uuid) or version < 0 or version > 0xFFFF:
raise ValueError ("Invalid Profile Descriptor")
for uuid in protocols:
if not is_valid_uuid (uuid):
raise ValueError ("invalid UUID specified in protocols")
try:
_bt.sdp_advertise_service (sock._sock, name, service_id, \
service_classes, profiles, provider, description, \
protocols)
except _bt.error as e:
raise BluetoothError (str (e))
I'm can't print off the client information if I don't advertise, and get a null pointer exception on the android side, so I figure it's necessary, but can't get past this error if I do advertise. This is the smallest amount of code I can have to get this error. Like I mentioned, not advertising results in no error, but I can't print off client information on the connect (the android side can't find the pi).
If you do know of a way to do this without that portion, here's the android code:
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);
}
}
}
My most recent Java side attempt (just in case the pi side was fine):
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);
}
I'd appreciate any help on this with getting a connection going. I'm not sure what's getting messed up here.
Looks like to advertise it just needed: "sudo hciconfig hci0 piscan"
And to connect it needed bluetooth.PORT_ANY from that import, so I'm marking this as answered.
To anyone that finds this because of their own problems with this. You have an answer now. Good luck.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With