Windows UWA(IoT)蓝牙RfcommService

时间:2016-04-24 09:28:17

标签: bluetooth win-universal-app iot

我正在尝试在两个Windows 10设备之间建立通信:

  1. Raspberry PI 2 B +蓝牙适配器
  2. Surface pro
  3. raspberry pi的服务器:

    private readonly Guid _rfcommChatServiceUuid = Guid.Parse("34B1CF4D-1069-4AD6-89B6-E161D79BE4D8");
    
    private async void CreateServer()
        {
            _provider = await RfcommServiceProvider.CreateAsync(RfcommServiceId.FromUuid(_rfcommChatServiceUuid));
            _listener = new StreamSocketListener();
    
            _listener.ConnectionReceived += OnConnectionReceived;
            await _listener.BindServiceNameAsync(_provider.ServiceId.AsString(),SocketProtectionLevel.BluetoothEncryptionAllowNullAuthentication);
    
            InitializeServiceSdpAttributes(_provider);
            _provider.StartAdvertising(_listener);
        }
    
        const uint SERVICE_VERSION_ATTRIBUTE_ID = 0x0300;
        const byte SERVICE_VERSION_ATTRIBUTE_TYPE = 0x0A;   // UINT32
        const uint SERVICE_VERSION = 200;
        void InitializeServiceSdpAttributes(RfcommServiceProvider provider)
        {
            var writer = new DataWriter();
            writer.WriteByte(SERVICE_VERSION_ATTRIBUTE_TYPE);
            writer.WriteUInt32(SERVICE_VERSION);
            var data = writer.DetachBuffer();
            provider.SdpRawAttributes.Add(SERVICE_VERSION_ATTRIBUTE_ID, data);
        }
    
     async void OnConnectionReceived(
    StreamSocketListener listener,
    StreamSocketListenerConnectionReceivedEventArgs args)
        {
    
            _provider.StopAdvertising();
            _listener.Dispose();
            _listener = null;
            _socket = args.Socket;
    

    表面的客户:

       var services = await DeviceInformation.FindAllAsync(
                  RfcommDeviceService.GetDeviceSelector(RfcommServiceId.FromUuid(_rfcommChatServiceUuid)));
    

    服务集合为空。

    这有什么问题?

    由于

1 个答案:

答案 0 :(得分:0)

我认为值得尝试在客户端硬编码地址。服务器和客户端都有sample C code个RFCOMM连接。

适用于Windows(通用)的蓝牙RFCOMM聊天示例为here