如何让您的 Voyager Pro UC 发送推文





0/5 (0投票)
如何让你的 Voyager Pro UC 发推文。
当我刚开始在缤特力工作时,我的老板给了我一个 Voyager Pro UC 耳机和一个下载 SDK 的链接,并说“给我做点什么”。这个思维实验的结果就是这篇博文和一些示例代码供你把玩。
读完这篇博文后,你应该会拥有一个自己的应用程序,可以让你访问缤特力耳机中的上下文信息并向全世界发推文。
让我们开始吧 ...
引导你的开发环境
以下是我假设你正在开发的某些东西
- Microsoft Visual Studio 2010
- C#
这是你需要准备的清单
- 缤特力 Spokes SDK:Spokes SDK 2.6 版
- Microsoft Visual Studio 的 NuGet:https://nuget.net.cn/
- TweetSharp 库 - 可通过 NuGet 安装:https://github.com/danielcrenna/tweetsharp
安装 Spokes SDK
SDK 的默认安装位置是 C:\Program Files\Plantronics\PlantronicsSDK。缤特力 Spokes 运行时和支持的开发人员库位于此文件夹中。
启动 Microsoft Visual Studio
创建一个 .NET 3.5 C# 类库项目 - 在此示例中,我创建了一个名为 TweetMe 的项目
项目创建后,将默认的 Class1.cs 重命名为 TweetMe.cs
添加 缤特力 和 TweetSharp 依赖项
获取 TweetSharp
NuGet 可以在 Visual Studio 的“项目”菜单下找到。要将 TweetSharp 添加到你的项目,请单击“项目”菜单并选择“管理 NuGet 包”选项。NuGet 对话框显示后,搜索 TweetSharp 库并将其添加到你的项目。
添加缤特力 SDK 引用
此应用程序需要两个 缤特力 程序集,即 Plantronics.Device.Common.dll 和 Plantronics.UC.Common.dll。这些程序集位于安装目录 C:\Program Files\Plantronics\PlantronicsSDK 中。要添加引用,请使用 Visual Studio 中传统的引用添加机制。
一旦你引用了 TweetSharp 和 缤特力 程序集,你的项目引用列表应该如下所示
编写缤特力插件
开发环境启动并运行后,你现在就可以开始编码了。
我们在这里要创建的是 缤特力 Spokes 运行时引擎的插件。有关 Spokes 的更多信息,请参阅 Spokes SDK 2.7 文档
步骤 1:创建一个基本的 IPlugin 实现。
IPlugin
接口有三个必须实现的方法:Name
、Init
和 Exit
,下面是空插件实现的骨架。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Plantronics.UC.Common;
using Plantronics.Device.Common;
namespace TweetMe
{
public class TweetMe : IPlugin
{
//Begin IPlugin Interface Implementations
//returns the name of the plugin
public string Name
{
}
//Called by the Plantronics Runtime to initialize the plugin
public bool Init()
{
return true;
}
//Called by the Plantronics Runtime when exiting
public void Exit()
{
}
//End IPlugin Interface Implementations
}
}
步骤 2. 添加缤特力对象
开始用我们将用于访问耳机事件的 缤特力 对象填充我们的插件骨架。
....
public class TweetMe : IPlugin
{
//Plantronics session classes
ISessionManager sessionManager = null;
ISession session = null;
//Plantronics device
IDevice myVoyagerPro = null;
IDeviceEvents myVoyagerProEvents = null;
IHostCommandExt command = null;
//Begin IPlugin Interface Implementations
//returns the name of the plugin
public string Name
{
get { return "Tweet Me Plugin"; }
}
//Called by the Plantronics Runtime to initialize the plugin
public bool Init()
{
//get the session manager instance
sessionManager = SessionManager.Instance;
if (sessionManager == null)
{
//error case return false;
return false;
}
return true;
}
//Called by the Plantronics Runtime when exiting
public void Exit()
{...
步骤 3:添加事件处理程序以接收 SessionManager 对象事件
SessionManager
对象知道 缤特力 耳机何时连接、已连接、正在断开连接或已断开连接。此事件处理程序将使用 SessionManager
的设备添加和移除事件,为我们的代码提供一些健壮性,允许插件随着耳机的连接、断开连接或重新连接而适应。
//Called by the Plantronics Runtime to initialize the plugin
public bool Init()
{
//get the session manager instance
sessionManager = SessionManager.Instance;
if (sessionManager == null)
{
//error case return false;
return false;
}
//regiester with the session manager to get notification when a Plantronics connects/disconnects
sessionManager.DeviceStateChanged +=new DeviceStateEventHandler(MyVoyagerProOnOffHandler);
return true;
}
...
//End IPlugin Interface Implementations
//Begin event handler code
public void MyVoyagerProOnOffHandler(object sender, DeviceStateEventArgs deviceStateEventArgs)
{
switch (deviceStateEventArgs.State)
{
case DeviceState.Added:
//do something with the added device
break;
case DeviceState.Removed:
//do some cleanup
break;
default:
break;
}
}
步骤 4:获取已连接的缤特力耳机
这里添加的代码将尝试从 Session
对象获取活动设备(耳机),并使用 SessionManager
激活插件。如果找到设备,我们将注册耳机事件通知。
此外,此代码片段还显示了 SessionManager
设备状态更改事件处理程序的实现。这里的实现非常简单,当耳机连接到主机时,我们希望注册来自耳机的事件;当耳机从主机断开连接时,我们希望清除任何以前的设备事件注册。
//Called by the Plantronics Runtime to initialize the plugin
public bool Init()
{
//get the session manager instance
sessionManager = SessionManager.Instance;
if (sessionManager == null)
{
//error case return false;
return false;
}
//regiester with the session manager to get notification when a Plantronics connects/disconnects
sessionManager.DeviceStateChanged +=new DeviceStateEventHandler(MyVoyagerProOnOffHandler);
//register the plugin with session from the session manager
session = sessionManager.Register(Name);
if (session == null)
{
//error case
return false;
}
//tell the session manager we are active
sessionManager.IsActive(Name, true);
//get my connected Plantronics headset
GetMyVoyagerPro();
return true;
}
...
//Begin my plantronics voyager pro methods
//sets the active plantronics device
private void GetMyVoyagerPro()
{
if (myVoyagerPro != null)
{
UnregisterMyVoyagerPro();
}
myVoyagerPro = session.ActiveDevice;
RegisterMyVoyagerProForEvents();
}
private void RegisterMyVoyagerProForEvents()
{
}
private void UnregisterMyVoyagerPro()
{
}
//End my voyager pro methods
//Begin event handler code
public void MyVoyagerProOnOffHandler(object sender, DeviceStateEventArgs deviceStateEventArgs)
{
switch (deviceStateEventArgs.State)
{
case DeviceState.Added:
GetMyVoyagerPro();
break;
case DeviceState.Removed:
UnregisterMyVoyagerPro();
break;
default:
break;
}
}
步骤 5:实现缤特力耳机事件注册逻辑
要从 Device
对象接收传感器事件,需要向活动设备注册一个 HeadsetStateChanged
事件处理程序。下面的代码展示了如何做到这一点,以及在我们完成设备操作后如何取消注册事件处理程序
值得注意的是,设备默认禁用近距离检测,无论何时我们注册接收设备事件,近距离都需要启用(如果我们需要该事件)。代码片段包含一个名为 EnableProximity
的方法,该方法会开启此功能。
//sets the active plantronics device
private void GetMyVoyagerPro()
{
if (myVoyagerPro != null)
{
UnregisterMyVoyagerPro();
}
myVoyagerPro = session.ActiveDevice;
RegisterMyVoyagerProForEvents();
}
private void RegisterMyVoyagerProForEvents()
{
if (myVoyagerPro == null)
{
return;.
}
//turn on proximity events
EnableProximity();
myVoyagerProEvents = myVoyagerPro.DeviceEvents;
(myVoyagerProEvents as IDeviceEventsExt).HeadsetStateChanged += new HeadsetStateEventHandler(MyVoyagerProStateChangeListener);
}
private void EnableProximity()
{
if (myVoyagerPro != null)
{
command = myVoyagerPro.HostCommand as IHostCommandExt;
if (command == null)
{
return;
}
//turn on proximity
command.EnableProximity(true);
//tell myVoyagerPro to send the proximity events
command.GetProximity();
}
}
private void UnregisterMyVoyagerPro()
{
(myVoyagerProEvents as IDeviceEventsExt).HeadsetStateChanged -= MyVoyagerProStateChangeListener;
myVoyagerPro = null;
}
//End my plantronics methods
//Begin event handler code
//handle events from the headset
public void MyVoyagerProStateChangeListener(object sender, HeadsetStateEventArgs myVoyagerProEventArgs){
}
public void MyVoyagerProOnOffHandler(object sender, DeviceStateEventArgs deviceStateEventArgs)
{ ...
步骤 6:添加 Twitter 对象
现在我们已经有了获取活动的 缤特力 耳机并注册耳机事件的基本管道,是时候插入 TweetSharp
对象了,这样我们就可以向全世界发推文,告诉他们你正在佩戴支持传感器的耳机。
为了以编程方式访问我的 @myvoyagerpro 帐户,我在 Twitter 上创建了一个 Twitter 应用程序。此应用程序可以使用 Twitter OAuth API 代表我发布推文。由于 Twitter 应用程序密钥应该是保密的,我已将其从该代码片段中省略,要获取你自己的 Twitter 应用程序访问密钥,请通过 Twitter 开发者网站 https://dev.twitter.com 创建你自己的应用程序。
注意:代码片段还显示了插件在 Exit
方法中清理代码的实现。
...
using TweetSharp;
namespace TweetMe
{
public class TweetMe : IPlugin
{
//Plantronics session classes
...
//twitter integration stuff
String twitterConsumerKey = "yourconsumerkey";
String twitterConsumerSecret = "yourconsumersecret";
String twitterAccessToken = "youraccesstoken";
String twitterAccessTokenSecret = "youraccesstokensecret";
String twitterUserHandle = "@yourtwitterhandle";
TwitterService twitter = null;
//Begin IPlugin Interface Implementations
...
//Called by the Plantronics Runtime to initialize the plugin
public bool Init()
{
...
//get my connected Plantronics
GetMyVoyagerPro();
//initialize twitter
InitializeTwitter();
return true;
}
//Called by the Plantronics Runtime when exiting
public void Exit()
{
//clean up
UnregisterMyVoyagerPro();
sessionManager.UnRegister(session);
twitter.EndSession();
}
//End IPlugin Interface Implementations
//twitter initialization
private void InitializeTwitter()
{
//initialize twitter
twitter = new TwitterService(twitterConsumerKey, twitterConsumerSecret);
twitter.AuthenticateWith(twitterAccessToken, twitterAccessTokenSecret);
}
步骤 7:将耳机事件转换为推文
下面 MyVoyagerProStateChangeListener
的实现是将耳机事件转换为推文的地方。虽然缤特力耳机可以发送许多上下文事件,这些事件可以被接收和处理,但这个插件故意保持简单,只关注以下事件
- HeadsetState.Don - 用户佩戴耳机的电容传感器事件
- HeadsetState.Doff - 用户取下耳机的电容传感器事件
- HeadsetState.InRange - 耳机在主机系统范围内的事件
- HeadsetState.OutofRange - 耳机超出主机系统范围的事件
- Proximity.Near - 耳机靠近主机系统的事件
- Proximity.Far - 耳机远离主机系统(但仍在范围内)的事件
//Begin event handler code
public void MyVoyagerProStateChangeListener(object sender, HeadsetStateEventArgs myVoyagerProEventArgs)
{
switch (myVoyagerProEventArgs.State)
{
//there are many other change events we can receive - this example just shows capturing wear state
case HeadsetState.Don:
//The user is wearing thier Plantronics
twitter.SendTweet("At " + DateTime.Now.ToString("H:mm:ss") + " my Sensors told me that " + twitterUserHandle + " is wearing me!");
break;
case HeadsetState.Doff:
//the user is not wearing their Plantronics
twitter.SendTweet("At " + DateTime.Now.ToString("H:mm:ss") + " my sensors told me that " + twitterUserHandle + " has taken me off.");
break;
case HeadsetState.InRange:
//the user is near their connection point
twitter.SendTweet("At " + DateTime.Now.ToString("H:mm:ss") + " my sensors told me that " + twitterUserHandle + " is in the building.");
//proximity as it has to be re-enabled when the headset comes into range
enableProximity();
break;
case HeadsetState.OutofRange:
//The user is away from their connection point
twitter.SendTweet("At " + DateTime.Now.ToString("H:mm:ss") + " my sensors told me that " + twitterUserHandle + " is out of the building");
break;
default:
break;
}
//check for proximity event state
switch (myVoyagerProEventArgs.Proximity)
{
case Proximity.Far:
// device is far
twitter.SendTweet("At " + DateTime.Now.ToString("H:mm:ss") + " my sensors told me that " + twitterUserHandle + " is away from their desk.");
break;
case Proximity.Near:
twitter.SendTweet("At " + DateTime.Now.ToString("H:mm:ss") + " my sensors told me that " + twitterUserHandle + " is at their desk.");
// device is near
break;
}
}
步骤 9:配置插件以在缤特力运行时环境 Spokes 中运行。
缤特力 Spokes 运行时具有可通过 XML 文件配置的可插拔架构。
以下 XML 片段是我们将添加到 Spokes 配置文件中的内容。
<!-- XML Fragment for PURE.xml -->
<Plugin Name="TweetMe" ProcessName="" Enabled="true" Active="false">
<Assembly>TweetMe.dll</Assembly>
<ClassType/>
<Type>SoftPhone</Type>
<FeatureType>0</FeatureType>
</Plugin>
要配置 Spokes,我们将导航到缤特力 SDK 安装目录并编辑 PURE.xml 文件,添加上面详述的 XML 片段。
配置 PURE.xml 文件后,请确保你的程序集已复制到缤特力 SDK 安装目录。
对于我的应用程序,我将以下程序集添加到了我的 SDK 文件夹中
- Hammock.ClientProfile.dll - TweetSharp 使用
- Hammock.dll - TweetSharp 使用
- Newtonsoft.Json.dll - TweetSharp 使用
- TweetSharp.dll - TweetSharp 库
- TweetMe.dll - 我们创建的 Spokes 插件
步骤 10:开始发推
在缤特力 SDK 安装目录中有一个名为 PlantronicesURE.exe 的可执行文件,这是 Spokes 运行时引擎服务。在你测试新插件之前,请检查是否没有正在运行此服务的实例。启动 PlantronicsURE.exe,你就应该可以发推文了。要测试你的代码是否有效,请戴上你的缤特力 Voyager Pro UC 并检查你的 Twitter 页面。
步骤 10b。如果我没有耳机怎么办?
不用担心,缤特力 SDK 附带了一个全面的耳机模拟器,如果你没有我们的耳机,可以使用它。有关模拟器的更多信息,请点击此处 缤特力设备模拟器文档。
源代码
好了,就是这样... 我希望这能对你有所帮助,如果你遇到任何问题,请告诉我!
祝好,
-Cary
本文由 Cary Bran 撰写。Cary 在位于华盛顿州西雅图的自家后院小木屋里为 缤特力 工作。他的职位是高级软件和架构总监。这意味着他领导下一代软件和设备概念的研发。
自 1998 年以来,他一直在统一通信和协作领域开发解决方案。最近,他曾担任软件架构师、技术策略师,并为 IETF 和 W3C WebRTC 标准化工作做出贡献。他拥有计算机科学学士学位和华盛顿大学福斯特商学院的 MBA 学位。