今天給大家介紹下 windows phone 8 的近場通信技術,常用近場通信技術有幾種 NFC、藍牙(Bluetooth)、Wifi 以上三種都是在WP8的API中所支持的,NFC我個人感覺是一個可以讓人耳目一新的功能。而且NFC設備目前被很多手機廠商應用,目前NFC技術在手機上應用主要有以下五類。
升級到WP8必需知道的13個特性 系列文章目錄地址:http://www.linuxidc.com/Linux/2013-08/89003.htm
可能有人會問到關於NFC的安全問題,以及傳輸速度問題,這裡我也給大家列出NFC的特性來幫助大家了解NFC。
如何在我們的應用中使用NFC呢?下來我逐一給大家介紹。
首先 還是設置我們的 WMAppManifest.xml 文件標記我們應用需要是有近場通信技術
這裡我還選擇了NetWoking 是因為後面我還會使用 Bluetooth 和 TCP/IP (Wi-Fi)連接。
建立NFC的連接我們要用到 Windows.Networking.Proximity.ProximityDevice 我們可以使用 Windows.Networking.Proximity.ProximityDevice.GetDefault(); 來判斷手機硬件是否支持NFC。
private void InitializeProximityDevice()
{
if (mProximityDevice == null)
{
mProximityDevice = Windows.Networking.Proximity.ProximityDevice.GetDefault();
}
if (mProximityDevice != null)
{
mProximityDevice.DeviceArrived += ProximityDeviceArrived;
mProximityDevice.DeviceDeparted += ProximityDeviceDeparted;
AddToSysMsgList(MSG_NFC_EXISTS);
}
else { AddToSysMsgList(MSG_NFC_NOT_EXISTS); }
}
上面的代碼還看到了連個事件 DeviceArrived 和 DeviceDeparted 分別用於判斷一個NFC設備進入和離開我們設備的感應區域。
下面我列舉一個發送消息的code
ProximityDevice device = ProximityDevice.GetDefault();
// Make sure NFC is supported
if (device!= null)
{
long Id = device.PublishMessage("Windows.SampleMessageType", "Hello World!");
Debug.WriteLine("Published Message. ID is {0}", Id);
// Store the unique message Id so that it
// can be used to stop publishing this message
}
注冊接收消息的code
Windows.Networking.Proximity.ProximityDevice proximityDevice;
private void InitializeProximityDevice()
{
proximityDevice = Windows.Networking.Proximity.ProximityDevice.GetDefault();
if (proximityDevice != null) {
proximityDevice.DeviceArrived += ProximityDeviceArrived;
proximityDevice.DeviceDeparted += ProximityDeviceDeparted;
WriteMessageText("Proximity device initialized.\n");
}
else
{
WriteMessageText("Failed to initialized proximity device.\n");
}
}
private void ProximityDeviceArrived(Windows.Networking.Proximity.ProximityDevice device)
{
WriteMessageText("Proximate device arrived. id = " + device.DeviceId + "\n");
}
private void ProximityDeviceDeparted(Windows.Networking.Proximity.ProximityDevice device)
{
WriteMessageText("Proximate device departed. id = " + device.DeviceId + "\n");
}
// Write a message to MessageBlock on the UI thread.
private Windows.UI.Core.CoreDispatcher messageDispatcher = Window.Current.CoreWindow.Dispatcher;
async private void WriteMessageText(string message, bool overwrite = false)
{
await messageDispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal,
() =>
{
if (overwrite)
MessageBlock.Text = message;
else
MessageBlock.Text += message;
});
}
這裡WP8除了支持信息的傳輸還支持更多的文件類型
Windows.Networking.Proximity.ProximityDevice proximityDevice;
private void PublishLaunchApp()
{
proximityDevice = Windows.Networking.Proximity.ProximityDevice.GetDefault();
if (proximityDevice != null)
{
// The format of the app launch string is: "<args>\tWindows\t<AppName>".
// The string is tab or null delimited.
// The <args> string can be an empty string ("").
string launchArgs = "user=default";
// The format of the AppName is: PackageFamilyName!PRAID.
string praid = "MyAppId"; // The Application Id value from your package.appxmanifest.
string appName = Windows.ApplicationModel.Package.Current.Id.FamilyName + "!" + praid;
string launchAppMessage = launchArgs + "\tWindows\t" + appName;
var dataWriter = new Windows.Storage.Streams.DataWriter();
dataWriter.UnicodeEncoding = Windows.Storage.Streams.UnicodeEncoding.Utf16LE;
dataWriter.WriteString(launchAppMessage);
var launchAppPubId =
proximityDevice.PublishBinaryMessage(
"LaunchApp:WriteTag", dataWriter.DetachBuffer());
}
}
詳細請參考 如果你使用Nokia的NDEF 請參考
其中包含了 配對請求,Tag信息的寫入,以及設備間交互。
其中交互功能可以使用連接deeplink的形式打開應用傳入參數或者打開應用或者應用商店。參考我之前的帖子:Windows Phone 8 中的應用間通信 http://www.linuxidc.com/Linux/2013-08/89004.htm
從 Windows Phone7 到 Windows Phone 8 更新 如何設配兩個版本
Windows Phone 8 與 Windows 8 開發技術概覽
Windows Phone & Windows 8 Push Notification from Windows Azure
在Windows Phone Store 商店中提交你的 Windows Phone 8 應用
Windows Phone 8 語音 - Speech for Windows Phone 8
Windows Phone 8 鏡頭應用 - Lenses for Windows Phone 8