javaiosbluetoothspp

IOS bluetooth serial connection to a remote PC/MAC


I have read about several questions about similar issue. But most of them are related with 3rd party bluetooth device connections. What I need is, to establish a bluetooth serial connection between IOS and a server applet which is waiting for a connection. This applet is supposed to run on Windows or MAC OS. Here is the Java code of the server applet:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import static my.remote.RemoteUI.OSName;
import javax.bluetooth.*;
import javax.microedition.io.*;
import static my.remote.ProcessInput.line;
import static my.remote.RemoteUI.OSName;

/**
* Class that implements an SPP Server which accepts single line of
* message from an SPP client and sends a single line of response to the client.
*/
public class Sspserver implements Runnable {
    private static boolean Connected = true;
    //start server
    private void startServer() throws IOException{

        //Create a UUID for SPP
        UUID uuid = new UUID("1101", true);
        //Create the servicve url
        String connectionString = "btspp://localhost:" + uuid +";name=Sample SPP Server";

        //open server url
        StreamConnectionNotifier streamConnNotifier = (StreamConnectionNotifier)Connector.open( connectionString );

        //Wait for client connection
        System.out.println("\nServer Started. Waiting for clients to connect...");
        StreamConnection connection=streamConnNotifier.acceptAndOpen();

        RemoteDevice dev = RemoteDevice.getRemoteDevice(connection);
        System.out.println("Remote device address: "+dev.getBluetoothAddress());
        System.out.println("Remote device name: "+dev.getFriendlyName(true));

        //read string from spp client
        InputStream inStream=connection.openInputStream();
        BufferedReader bReader=new BufferedReader(new InputStreamReader(inStream));

        while(Connected){
            try {
                String lineRead=bReader.readLine();
                System.out.println(lineRead);
                String com = lineRead.substring(0, lineRead.length());
                CommandActivity command = new CommandActivity();
                if ((lineRead != null) && (OSName != null)) {
                    command.Command(com,OSName);
                    System.out.println(com);
                }
            }catch(Exception e) {
                return;
            }
        }

    }

    @Override
    public void run() {

        try {
            //display local device address and name
            LocalDevice localDevice = LocalDevice.getLocalDevice();
            System.out.println("Address: "+localDevice.getBluetoothAddress());
            System.out.println("Name: "+localDevice.getFriendlyName());

            //Sspserver sampleSPPServer=new Sspserver();
            startServer();
        } catch (BluetoothStateException ex) {
            System.out.println(ex.getMessage());
        } catch (IOException ex) {
            System.out.println(ex.getMessage());
        }

    }

}

CommandActivity class is just parsing the line of data coming from device. Android devices can easily connect to this server using PC's MAC address. But for IOS I couldn't find any solution for socket connection for bluetooth.

I tried EAAccessoryManager but unfortunately IOS device couldn't detect PC as an accessory. It needs Supported external accessory protocols definitions but I can't find all PC or MAC protocols.

Also I tried CoreBluetooth. But this applet is not advertising name or UUID of services/characteristics.

There must be an easy way which I may be missing. A simple serial connection shouldn't be a problem for an operating system which has lots of features like IOS. Any suggestion appreciated. Thanks.


Solution

  • You cannot estalish a serial port connection from an iOS app over Bluetooth unless the device you are connecting to is MFi certified, which lets you use the external accessory framework. Since your PC is not an MFi certified device, it won't work.

    You will either need your PC to advertise as a BLE device so that you can use Core Bluetooth or use an alternative connection method, such as TCP/IP via wifi.