DEVELOPERS BLOG

Directions for Wi-Fi Direct Connection with Your App

BlackBerry WiFi Direct_large

Wi-Fi Direct is a new method of communication now available to BlackBerry users. First introduced on the BlackBerry Z30 running OS 10.2, but also coming on the BlackBerry Q5 and Q10 in 10.2, Wi-Fi Direct allows for communication from one device to another without the need for any network infrastructure. You can use this to connect to other users and share media files or play a multiplayer game. It can also be used to communicate with hardware devices such as printers or digital cameras that also support Wi-Fi Direct.

In BlackBerry OS 10.2, we support a 1:1 connection over Wi-Fi Direct, meaning one device can communicate with another. However, Wi-Fi Direct also allows for multiple devices to communicate Wi-Fi Direct Group. So -in BlackBerry OS 10.2.1, we’ll be supporting Wi-Fi Direct groups of up to 8 devices.

A BlackBerry user can configure a Wi-Fi Direct connection themselves by going to Settings > Network Connections > Wi-Fi and then choosing Wi-Fi Direct. This initiates a scan for other Wi-Fi Direct devices. While scanning, the BlackBerry Smartphone is also discoverable by other Wi-Fi Direct devices.

Now you wouldn’t want to force a user to have to do this to make a connection for your application, right? This is the BlackBerry Developer Blog, so let’s learn how we can use the WiFiDirect API to make the connection programmatically right within your app!

If you want to jump right in and try out Wi-Fi Direct in an application, have a look at the WiFiDirect-ion sample application I created. You’ll need two devices to try it out. The table below shows screenshots of what you’ll see at each step:

  1. After starting the application, choose Operator on one device and Automatron on the other.
  2. This triggers a Wi-Fi Direct scan on both devices.
  3. Once a connection is found, it will be listed on the automatron device. Touch the found device to establish the connection and agree to accept the connection on the operator device.
  4. Once the connection is established, you’ll see a control pad on the operator device. Touch an arrow and it’ll appear on the automatron device.

WiFiDirect-ion Screenshots

Operator Device

Automatron Device

  WiFi Direct Screen Shots_Operator_1  WiFi Direct Screen Shots_Automatron_1
 WiFi Direct Screen Shots_Operator_2  WiFi Direct Screen Shots_Automatron_2
 WiFi Direct Screen Shots_Operator_3  WiFi Direct Screen Shots_Automatron_3
 WiFi Direct Screen Shots_Automatron_4
WiFi Direct Screen Shots_Operator_5 WiFi Direct Screen Shots_Automatron_5

Let’s take a look at what the application is doing under the covers. The first step to establish the connection is to call requestScan to scan for other Wi-Fi Direct devices. A scan lasts for one minute and makes the device discoverable to other Wi-Fi Direct devices while scanning is in progress. So if you need to connect 2 BlackBerry Smartphones, you’ll need to initiate the scan on both. When a device is found, the deviceFound signal is fired, which provides the name of the device, its hardware address and WiFiDirectDeviceType. You can either present the list of devices to the user to choose, or connect automatically if your application is looking for a specific device.

Once you’ve chosen the device to connect to, call requestConnect to initiate the connection. The requestConnect method allows the application to choose the Wi-Fi Protected Setup method to establish the connection. There are three options: Push Button Configuration, Random PIN and Preset PIN. The table below describes the behavior for each when connecting two BlackBerry Smartphones.

Wi-Fi Protected Setup Method Device Initiating Connection Device Being Connected To
Push Button Configuration A prompt is shown while the remote user accepts the connection and the connection is being established. A prompt to accept the connection is shown.
Random PIN A prompt is shown which contains a random PIN that must be entered on the remote device. A prompt to enter the PIN to establish the connection is shown.
Preset PIN A prompt is shown which contains the PIN specified by the application that must be entered on the remote device. A prompt to enter the PIN to establish the connection is shown.

Once the Wi-Fi Direct connection has been established, the deviceConnected signal is fired. The deviceConnected signal supplies you with information about the device that just connected, including the device name, hardware address, IP address and WiFiDirectIntent. At this point, your application should call requestStartSession to keep the Wi-Fi Direct session up. If there are no active sessions for two minutes, the Wi-Fi Direct radio will be disabled in order to conserve power.

If you’ve made it this far, your application is ready to communicate over the Wi-Fi Direct connection using any IP-based protocol. Once you are finished with the connection, call requestStopSession to remove your session from the pool, allowing the Wi-Fi Direct radio to power down if there are no other active sessions.

If you have any questions about how to use Wi-Fi Direct in your application, come to our BlackBerry Developer Forums and ask!

Mark Sohm

About Mark Sohm

Senior Technical Solutions Manager on the Solution Architects team.

Mark Sohm joined BlackBerry in 2003 and currently works as a Senior Technical Solutions Manager on the Solutions Architects team. Mark Sohm has been helping developers create applications using BlackBerry technologies for over 15 years, starting way back with the very first BlackBerry JDK on BlackBerry OS 3.6 through to BlackBerry 10 and now Android with BlackBerry Dynamics and Android Enterprise.