none
关于wp8蓝牙串口编程 RRS feed

  • 问题

  • 有一块蓝牙串口模块,想与手机连接并通讯,但是连接过程中报0x80070005 Access is denied错误。不知道怎么解决,谁可以帮帮我啊。
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Net;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Navigation;
    using Microsoft.Phone.Controls;
    using Microsoft.Phone.Shell;
    using shiyan2.Resources;
    using Windows.Storage.Streams;
    using Windows.Networking.Sockets;
    using Windows.Networking.Proximity;
    
    
    namespace shiyan2
    {
        public partial class MainPage : PhoneApplicationPage
        {
            //private StreamSocket _socket = null;  // Socket数据流对象
            private DataWriter _dataWriter;  // 数据写入对象
           // private DataReader _dataReader; // 数据读取对象
           // string datasendOK;
            // 构造函数
            public MainPage()
            {
                InitializeComponent();
                info_print.Text = "启动:OK!\r\n";
                // 用于本地化 ApplicationBar 的示例代码
                //BuildLocalizedApplicationBar();
                
            }
    
            private async void Fbutton_Click(object sender, RoutedEventArgs e)
            {
                 
                try
                  {
                     //开始查找对等项
                     // PeerFinder.Start();
                   
                     PeerFinder.AlternateIdentities["Bluetooth:Paired"] = "";
                    //  PeerFinder.AlternateIdentities["Bluetooth:SDP"] = "00001101-0000-1000-8000-00805F9B34FB";   //这一步是正确的
                      var peers =await PeerFinder.FindAllPeersAsync();
                      if (peers.Count == 0)
                      {
                         info_print.Text += "没有发现对等的蓝牙应用\r\n";
                      }
                      else
                      {
                          // 把对等项目绑定到列表中
                        
                          foreach (PeerInformation i in peers)
                          {
                              info_print.Text += "搜索到设备:\r\n" + i.DisplayName + "," + i.Id + "," + i.HostName + ","+i.ServiceName + ".\r\n";
                               
                          }
                          PeerInformation peerInfo =peers .FirstOrDefault(c => c.DisplayName.Contains("HC"));
                          StreamSocket _socket =new StreamSocket ();
                          
                         info_print.Text += "正在尝试连接:" + peerInfo.DisplayName+"。\r\n";
                         await _socket.ConnectAsync(peerInfo.HostName, "{00001101-0000-1000-8000-00805F9B34FB}");
                          }
                   }
                  catch (Exception ex)
                  {   
                      if ((uint)ex.HResult == 0x8007048F)
                      {
                          //MessageBox.Show("本地蓝牙关闭,请打开蓝牙重试!");
                          //Windows.System.Launcher.LaunchUriAsync(new Uri("ms-settings-bluetooth"));
                          info_print.Text += "本地蓝牙关闭,请打开蓝牙重试!" + "\r\n";
                      }
                      else
                      //    MessageBox.Show(ex.Message );
                      info_print.Text += ex.Message + "\r\n";
                    
                  }     
    }
    
    
    
    
            /*DataWriter dataWriter;
            private async void  SendMessage(string message)
            {
                if (_dataWriter == null) 
                    _dataWriter = new DataWriter(_socket.OutputStream); 
                _dataWriter.WriteInt32(message.Length);
                await _dataWriter.StoreAsync();
                _dataWriter.WriteString(message);    
                await _dataWriter.StoreAsync();
             //   datasendOK = "datasendOK";
            } */
    
          /*  
    private byte[] getHexBytes(String message)
    {
            int len = message.Length / 2;
            char[] chars = message.ToCharArray();
            String[] hexStr = new String[len];
            byte[] bytes = new byte[len];
            for (int i = 0, j = 0; j < len; i += 2, j++) {
                hexStr[j] = "" + chars[i] + chars[i + 1];
                bytes[j] = (byte) Integer.parseInt(hexStr[j], 16);
            }
            return bytes;
        }
     
    */
            
        }
    }
    2014年9月5日 13:24

答案

  • 楼主 你好,

    1. 首先我们要确保在manifest文件中已经启用ID_CAP_NETWORKING,ID_CAP_PROXIMITY这两个选项。

    2. 另外推荐你参考:http://blogs.msdn.com/b/flecoqui/archive/2013/12/13/windows-store-and-windows-phone-app-to-app-communication-over-bluetooth.aspx

    在这篇文章的评论中 Maxime DENIS 遇到相同的问题,通过下面的方式解决了,

     <Capabilities>
       <Capability Name="internetClientServer" />
       <DeviceCapability Name="proximity" />
       <m2:DeviceCapability Name="bluetooth.rfcomm">
         <m2:Device Id="any">
           <m2:Function Type="serviceId:xxxxxxxx" />
         </m2:Device>
       </m2:DeviceCapability>
     </Capabilities>
    谢谢!

    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    2014年9月8日 1:41
    版主

全部回复

  • 楼主 你好,

    1. 首先我们要确保在manifest文件中已经启用ID_CAP_NETWORKING,ID_CAP_PROXIMITY这两个选项。

    2. 另外推荐你参考:http://blogs.msdn.com/b/flecoqui/archive/2013/12/13/windows-store-and-windows-phone-app-to-app-communication-over-bluetooth.aspx

    在这篇文章的评论中 Maxime DENIS 遇到相同的问题,通过下面的方式解决了,

     <Capabilities>
       <Capability Name="internetClientServer" />
       <DeviceCapability Name="proximity" />
       <m2:DeviceCapability Name="bluetooth.rfcomm">
         <m2:Device Id="any">
           <m2:Function Type="serviceId:xxxxxxxx" />
         </m2:Device>
       </m2:DeviceCapability>
     </Capabilities>
    谢谢!

    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    2014年9月8日 1:41
    版主
  • 谢谢您的帮助,我根据您的提示已完成设备连接。
    2014年9月16日 15:12