【问题标题】:IOS bluetooth serial connection to a remote PC/MACIOS 蓝牙串行连接到远程 PC/MAC
【发布时间】:2017-04-28 10:51:13
【问题描述】:

我已阅读有关类似问题的几个问题。但其中大部分都与第 3 方蓝牙设备连接有关。我需要的是,在 IOS 和等待连接的服务器小程序之间建立蓝牙串行连接。这个小程序应该在 Windows 或 MAC OS 上运行。以下是服务器小程序的 Java 代码:

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 类只是解析来自设备的数据行。 Android 设备可以使用 PC 的 MAC 地址轻松连接到此服务器。但是对于 IOS,我找不到任何蓝牙套接字连接的解决方案。

我尝试了EAAccessoryManager,但不幸的是 IOS 设备无法将 PC 检测为附件。它需要Supported external accessory protocols 定义,但我找不到所有PC 或MAC 协议。

我也试过CoreBluetooth。但是这个小程序不是广告名称或服务/特性的 UUID。

一定有一个简单的方法,我可能会错过。对于具有许多功能(如 IOS)的操作系统来说,简单的串行连接应该不是问题。任何建议表示赞赏。谢谢。

【问题讨论】:

    标签: java ios bluetooth spp


    【解决方案1】:

    您无法通过蓝牙从 iOS 应用程序建立串行端口连接,除非您连接的设备经过 MFi 认证,这允许您使用外部附件框架。由于您的 PC 不是经过 MFi 认证的设备,因此无法正常工作。

    您需要将您的 PC 宣传为 BLE 设备,以便您可以使用核心蓝牙或使用其他连接方法,例如通过 wifi 的 TCP/IP。

    【讨论】:

      猜你喜欢
      • 2012-01-31
      • 1970-01-01
      • 1970-01-01
      • 2014-10-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-03-11
      • 2015-08-03
      相关资源
      最近更新 更多