tcoder
Posts: 20
Joined: Sun Sep 18, 2016 5:05 pm

bluetoothctl not connecting with android phone

Tue Aug 22, 2017 8:16 pm

I've written code in my phone to connect with the Raspberry Pi Zero W. Using bluetoothctl to test the code gets a "Connected: yes" then a "Connected: no" The phone and the Pi are already paired. I did a show command and here is the result:

Code: Select all

[bluetooth]# show
Controller B8:27:EB:A1:4D:B2
        Name: SprinKon
        Alias: SprinKon
        Class: 0x000000
        Powered: yes
        Discoverable: yes
        Pairable: yes
        UUID: PnP Information           (00001200-0000-1000-8000-00805f9b34fb)
        UUID: Generic Access Profile    (00001800-0000-1000-8000-00805f9b34fb)
        UUID: Generic Attribute Profile (00001801-0000-1000-8000-00805f9b34fb)
        UUID: A/V Remote Control        (0000110e-0000-1000-8000-00805f9b34fb)
        UUID: A/V Remote Control Target (0000110c-0000-1000-8000-00805f9b34fb)
        Modalias: usb:v1D6Bp0246d0517
        Discovering: no
[bluetooth]# agent on
Agent registered
[bluetooth]#    (Here, I use the phone to connect using the program that appears next. Every time I do a connect, I get the following.)
[CHG] Device D0:FC:CC:CE:8A:D7 Connected: yes
[CHG] Device D0:FC:CC:CE:8A:D7 Connected: no
Here is the code in the phone which gets a timeout error:

Code: Select all

BluetoothDevice device = null;
BluetoothSocket mSocket = null;
BluetoothAdapter btAdapter;

device = GetBluetoothDevice(btAdapter, "SprinKon");

if(device.getBondState()== device.BOND_BONDED)
{
  try
  {
    mSocket = device.createInsecureRfcommSocketToServiceRecord(MY_UUID);		 
  }
  catch (IOException e1)
  {		
    e1.printStackTrace();
  }
  try
  {
    mSocket.connect();
    Toast.makeText(getApplicationContext(), "Connect was successful.", Toast.LENGTH_LONG).show();
  }
  catch(IOException e)
  {
  try
  {
    mSocket.close();
    Toast.makeText(getApplicationContext(), "Cannot connect.", Toast.LENGTH_LONG).show();
  }
  catch (IOException e1)
  {
  }
}
I've also written a server program in C++ for the Pi that just sits, waiting for a connection from the phone. It never gets the connection.

Code: Select all

char buf[1024] = {0};	
int bluetoothSocket, client, bytes_read;		 
struct sockaddr_rc loc_addr = {0};
struct sockaddr_rc client_addr = {0};

socklen_t opt = sizeof(client_addr);	

bdaddr_t my_bdaddr_any = {0, 0, 0, 0, 0, 0};	
 
bluetoothSocket = socket(AF_BLUETOOTH, SOCK_STREAM, BTPROTO_RFCOMM);	 
 
loc_addr.rc_family = AF_BLUETOOTH;
loc_addr.rc_bdaddr = (bdaddr_t)my_bdaddr_any;		 		
loc_addr.rc_channel = (uint8_t) 1;

int ret = -1;

if ((ret = bind(bluetoothSocket, (struct sockaddr *)&loc_addr, sizeof(loc_addr))) == -1)	 
{
  printf("Bluetooth bind failed. ERRNO=%d\n", errno);
  char *errorMessage = strerror_r(errno, buf, 1024);
  printf("%s\n", errorMessage);
  return 0;
}
	
ret = -1;

if((ret = listen(bluetoothSocket, 1)) == -1)
{
  printf("Bluetooth listen failed. ERRNO=%d\n", errno);
  char *errorMessage = strerror_r(errno, buf, 1024);
  printf("%s\n", errorMessage);
  return 0;
}

while (stopThread == false)
{
  printf("Waiting for new client to connect.\n");
	
  client = accept(bluetoothSocket, (struct sockaddr *)&client_addr, &opt);
	
  if (client == -1)
  {
    printf("Bluetooth connect failed.\n");
    close(client);
    delay(1000);
    continue;
  }
			
  printf("Bluetooth connection made.\n");
	
  ba2str(&loc_addr.rc_bdaddr, buf);
  fprintf(stderr, "accepted connection from %s\n", buf);
  memset(buf, 0, sizeof(buf));

  // read data from the client
  bytes_read = read(client, buf, sizeof(buf));
  if (bytes_read > 0) 
  {
    printf("Bluetooth bytes received [%s]\n", buf);
  }
}			

close(client);
close(bluetoothSocket);
return 0;	

User avatar
Douglas6
Posts: 4091
Joined: Sat Mar 16, 2013 5:34 am
Location: Chicago, IL

Re: bluetoothctl not connecting with android phone

Wed Aug 23, 2017 1:06 am

See if the instructions in this post help: viewtopic.php?p=947185#p947185

tcoder
Posts: 20
Joined: Sun Sep 18, 2016 5:05 pm

Re: bluetoothctl not connecting with android phone

Wed Aug 23, 2017 3:46 pm

Thanks for the info. I already had those two items in the file dbus-org.bluez.service. However, I never tried the command "sudo rfcomm watch hci0". I also already had my phone paired with the bluetooth connection as shown:

Code: Select all

[bluetooth]# paired-devices
Device D0:FC:CC:CE:8A:D7 Samsung Galaxy S7
When I entered the command "sudo rfcomm watch hci0", I got:

Code: Select all

Waiting for connection on channel 1
I tried connecting from my phone and it never connected. Now the problem is, "what am I doing wrong in the Android code to connect?". This is pretty much straight forward code. I must be missing something.

tcoder
Posts: 20
Joined: Sun Sep 18, 2016 5:05 pm

Re: bluetoothctl not connecting with android phone

Wed Aug 23, 2017 4:10 pm

I forgot to mention that when I ran the bluetoothctl utility, and after it starts, every time I try and connect, I get

Code: Select all

[bluetooth]#  
Device D0:FC:CC:CE:8A:D7 Samsung Galaxy S7
[CHG] Device D0:FC:CC:CE:8A:D7 Connected: yes
[CHG] Device D0:FC:CC:CE:8A:D7 Connected: no
[CHG] Device D0:FC:CC:CE:8A:D7 Connected: yes
[CHG] Device D0:FC:CC:CE:8A:D7 Connected: no
[CHG] Device D0:FC:CC:CE:8A:D7 Connected: yes
[CHG] Device D0:FC:CC:CE:8A:D7 Connected: no
[bluetooth]#
That tells me that it is trying to connect, but something is not right, as it immediately disconnects. At least the connect is getting somewhere to the Pi bluetooth circuitry.

tcoder
Posts: 20
Joined: Sun Sep 18, 2016 5:05 pm

Re: bluetoothctl not connecting with android phone

Wed Aug 23, 2017 7:59 pm

I did some more testing and used the utility "list-devices" and got:

Code: Select all

root@SprinKon:/home/pi/bluez-5.37/test# ./list-devices
[ /org/bluez/hci0 ]
    Name = SprinKon
    Powered = 1
    Modalias = usb:v1D6Bp0246d0517
    DiscoverableTimeout = 180
    Alias = SprinKon
    PairableTimeout = 0
    Discoverable = 1
    Address = B8:27:EB:A1:4D:B2
    Discovering = 0
    Pairable = 1
    Class = 0
    UUIDs = 0x1200 0x1800 0x1801 0x110e 0x110c
    [ /org/bluez/hci0/dev_D0_FC_CC_CE_8A_D7 ]
        Name = Samsung Galaxy S7
        Paired = 1
        Modalias = bluetooth:v0075p0100d0200
        Adapter = /org/bluez/hci0
        LegacyPairing = 0
        Alias = Samsung Galaxy S7
        Connected = 0
        UUIDs = 0x1105 0x110a 0x110c 0x1112 0x1115 0x1116 0x111f 0x112f 0x1132 0x1200 0x1800 0x1801
        Address = D0:FC:CC:CE:8A:D7
        Blocked = 0
        Class = 0x5a020c
        Trusted = 1
        Icon = phone
I also found several utilities for my phone, one is called "BlueTerminal" which is written for testing Raspberry Pi's and other devices. It got the same results as the Android program that I wrote. Therefore it seems that the problem may lie in the Raspberry Pi Zero W somewhere or the BlueZ code. The C++ program I wrote is also using the BlueZ library. Maybe I should dig into the BlueZ code and find out why it gets a connect for a second or two and then disconnects.

tcoder
Posts: 20
Joined: Sun Sep 18, 2016 5:05 pm

Re: bluetoothctl not connecting with android phone

Wed Aug 23, 2017 8:27 pm

I was only using bluetoothctl to test the bluetooth in the Raspberry Pi and it was failing. However, I started up my C++ program and used the "BlueTerminal" program that I downloaded from Google and it worked. My guess at this time, is that the connect from the Android program I wrote wasn't waiting long enough for the connect signal, and so was timing out. Does anyone know of a way to increase the connect timer? I think that 1 or 2 seconds is unreasonable.

User avatar
Douglas6
Posts: 4091
Joined: Sat Mar 16, 2013 5:34 am
Location: Chicago, IL

Re: bluetoothctl not connecting with android phone

Wed Aug 23, 2017 9:46 pm

Run 'sudo rfcomm watch hci0' in a terminal on the Pi. It should reply with 'waiting for a connection'. Leave that window open. Now try to connect from BlueTerm. What does the rfcomm window on the Pi say? It should say something like 'connection established, press ctrl-C to hang up'. At that point, you can run minicom, picocom, or another program (in a separate terminal window), to open the /dev/rfcomm0 virtual serial port, and read and write to it.

tcoder
Posts: 20
Joined: Sun Sep 18, 2016 5:05 pm

Re: bluetoothctl not connecting with android phone

Wed Aug 23, 2017 10:17 pm

I started rfcom watch hci0 and connected with BlueTerminal then did a CTRL-Esc. I configured minicom to use /dev/rfcomm0, but when I start minicom, I get

Code: Select all

root@SprinKon:/home/pi# minicom
minicom: cannot open /dev/rfcomm0: No such file or directory
BTW, the return from the Android program I wrote on the connect() is “java.io.IOException: read failed, socket might closed or timeout, read ret: -1”

tcoder
Posts: 20
Joined: Sun Sep 18, 2016 5:05 pm

Re: bluetoothctl not connecting with android phone

Wed Aug 23, 2017 10:21 pm

Also, when I run picocom, I get:

Code: Select all

root@SprinKon:/home/pi# picocom /dev/rfcomm0
picocom v1.7

port is        : /dev/rfcomm0
flowcontrol    : none
baudrate is    : 9600
parity is      : none
databits are   : 8
escape is      : C-a
local echo is  : no
noinit is      : no
noreset is     : no
nolock is      : no
send_cmd is    : sz -vv
receive_cmd is : rz -vv
imap is        :
omap is        :
emap is        : crcrlf,delbs,

FATAL: cannot open /dev/rfcomm0: No such file or directory

tcoder
Posts: 20
Joined: Sun Sep 18, 2016 5:05 pm

Re: bluetoothctl not connecting with android phone

Wed Aug 23, 2017 10:24 pm

Ignore the last two posts. I screwed up.

tcoder
Posts: 20
Joined: Sun Sep 18, 2016 5:05 pm

Re: bluetoothctl not connecting with android phone

Wed Aug 23, 2017 10:26 pm

Ok, I was able to use picocom to send data to BlueTerminal with no problems. That works.

User avatar
Douglas6
Posts: 4091
Joined: Sat Mar 16, 2013 5:34 am
Location: Chicago, IL

Re: bluetoothctl not connecting with android phone

Wed Aug 23, 2017 10:41 pm

Good. You can run the rfcomm watch in the background at boot time. See this post: viewtopic.php?p=919463#p919463

tcoder
Posts: 20
Joined: Sun Sep 18, 2016 5:05 pm

Re: bluetoothctl not connecting with android phone

Thu Aug 24, 2017 3:05 pm

I think I got you confused. I'm not trying to connect via bluetooth serial, I'm trying to connect via normal bluetooth. The program that I tried that worked was "BlueTerminal", not "BlueTerm". "BlueTerm" is using bluetooth via a serial connection.

This issue should be considered closed, as the original problem was that my C++ program was not accepting a connection from an Android program. The C++ program has been found to be working, as it worked with the Android program "BlueTerminal". The problem is with my Android program that I wrote.

User avatar
Douglas6
Posts: 4091
Joined: Sat Mar 16, 2013 5:34 am
Location: Chicago, IL

Re: bluetoothctl not connecting with android phone

Tue Aug 29, 2017 7:55 pm

Glad you sorted your problem. BlueTerminal, like BlueTerm, is a serial Bluetooth terminal that runs on Android. Not sure what you mean by 'normal Bluetooth', but you're using Bluetooth serial communication.

ReachISO
Posts: 3
Joined: Mon Aug 28, 2017 7:17 am

Re: bluetoothctl not connecting with android phone

Mon Sep 04, 2017 10:09 am

You can retry to connect it.

tcoder
Posts: 20
Joined: Sun Sep 18, 2016 5:05 pm

Re: bluetoothctl not connecting with android phone

Mon Sep 04, 2017 1:40 pm

The problem was in the Android code. In the code I was doing

Code: Select all

UUID MY_UUID = UUID.fromString("00000003-0000-1000-8000-00805F9B34FB");
and I changed it to:

Code: Select all

UUID MY_UUID = device.getUuids()[0].getUuid();

Return to “Java”

Who is online

Users browsing this forum: No registered users and 7 guests