Page 98

Android Programming Cookbook

90 / 143

public void discoverable(View v) { ensureDiscoverable(); } }

5.5

Create the source code of the BluetoothChatService

The BluetoothChatService class does all the work for setting up and managing Bluetooth connections with other devices. It has a thread that listens for incoming connections, a thread for connecting with a device, and a thread for performing data transmissions when connected. So, add a new Java class inside src/com.javacodegeeks.androidBluetoothExample/ so that we are going to have the src/com.javacodegeeks.androidBluetoothExample/BluetoothChatService.java file and paste the code below. BluetoothChatService.java package com.javacodegeeks.androidBluetoothExample; import import import import import import import import

android.bluetooth.BluetoothAdapter; android.bluetooth.BluetoothDevice; android.bluetooth.BluetoothServerSocket; android.bluetooth.BluetoothSocket; android.content.Context; android.os.Bundle; android.os.Handler; android.os.Message;

import import import import

java.io.IOException; java.io.InputStream; java.io.OutputStream; java.util.UUID;

/** * This class does all the work for setting up and managing Bluetooth * connections with other devices. It has a thread that listens for * incoming connections, a thread for connecting with a device, and a * thread for performing data transmissions when connected. */ public class BluetoothChatService { // Name for the SDP record when creating server socket private static final String NAME = "BluetoothChat"; // Unique UUID for this application private static final UUID MY_UUID = UUID.fromString("fa87c0d0-afac-11de-8a39-0800200 �c9a66"); // Member fields private final BluetoothAdapter mAdapter; private final Handler mHandler; private AcceptThread mAcceptThread; private ConnectThread mConnectThread; private ConnectedThread mConnectedThread; private int mState; // Constants that indicate the current connection state public static final int STATE_NONE = 0; // we’re doing nothing public static final int STATE_LISTEN = 1; // now listening for incoming connections public static final int STATE_CONNECTING = 2; // now initiating an outgoing connection

SKY MAGZ  

This is Yahya Christian School Magazine

Read more
Read more
Similar to
Popular now
Just for you