Android Open Source - LibGeppa Bluetooth Passive Receiver






From Project

Back to project page LibGeppa.

License

The source code is released under:

Apache License

If you think the Android project LibGeppa listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package net.cattaka.libgeppa.passive;
/* www.j a  v a 2 s. c o m*/
import java.io.IOException;
import java.util.Set;

import net.cattaka.libgeppa.Constants;
import net.cattaka.libgeppa.IRawSocket;
import net.cattaka.libgeppa.bluetooth.IBluetoothAdapter;
import net.cattaka.libgeppa.bluetooth.IBluetoothDevice;
import net.cattaka.libgeppa.bluetooth.IBluetoothSocket;
import net.cattaka.libgeppa.data.IPacket;
import net.cattaka.libgeppa.data.IPacketFactory;
import net.cattaka.libgeppa.socket.BtRawSocket;
import net.cattaka.libgeppa.thread.ConnectionThread;
import net.cattaka.libgeppa.thread.ConnectionThread.IRawSocketPrepareTask;
import net.cattaka.libgeppa.thread.IConnectionThreadListener;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;

public class BluetoothPassiveReceiver<T extends IPacket> implements IPassiveReceiver<T> {
    private final BroadcastReceiver mBtConnReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            if (BluetoothAdapter.ACTION_STATE_CHANGED.equals(intent.getAction())) {
                // When Bluetooth adapter turned on, it starts ConnectionThread.
                int state = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE,
                        BluetoothAdapter.STATE_OFF);
                if (state == BluetoothAdapter.STATE_ON) {
                    mListener.startConnectionThread();
                }
            } else if (BluetoothDevice.ACTION_ACL_CONNECTED.equals(intent.getAction())) {
                // When Bluetooth device connected, it starts Connection thread.
                BluetoothDevice device = (BluetoothDevice)intent
                        .getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
                if (mTargetDeviceName.equals(device.getName())) {
                    mListener.startConnectionThread();
                }
            } else if (BluetoothDevice.ACTION_ACL_DISCONNECTED.equals(intent.getAction())) {
                // none
            }
        }
    };

    private IRawSocketPrepareTask mBluetoothPrepareTask = new IRawSocketPrepareTask() {
        @Override
        public IRawSocket prepareRawSocket() {
            Set<IBluetoothDevice> devices = mBluetoothAdapter.getBondedDevices();
            BtRawSocket rawSocket = null;
            for (IBluetoothDevice device : devices) {
                if (!mTargetDeviceName.equals(device.getName())) {
                    continue;
                }
                try {
                    IBluetoothSocket socket = device
                            .createRfcommSocketToServiceRecord(Constants.SPP_UUID);
                    socket.connect();
                    rawSocket = new BtRawSocket(socket, Constants.OUTPUT_BUF_SIZE,
                            device.getAddress());
                    break;
                } catch (IOException e) {
                    // ignore
                    // Log.d(Constants.TAG_DEBUG, e.getMessage(),
                    // e);
                }
            }
            return rawSocket;
        }
    };

    private Context mContext;

    private IPassiveReceiverListener mListener;

    private String mTargetDeviceName;

    private IPacketFactory<T> mPacketFactory;

    private IBluetoothAdapter mBluetoothAdapter;

    public BluetoothPassiveReceiver(IPassiveReceiverListener listener, String targetDeviceName,
            IBluetoothAdapter bluetoothAdapter) {
        super();
        mListener = listener;
        mTargetDeviceName = targetDeviceName;
        mBluetoothAdapter = bluetoothAdapter;
    }

    @Override
    public void onCreateService(Context context, IPacketFactory<T> packetFactory) {
        mContext = context;
        mPacketFactory = packetFactory;

        IntentFilter filter = new IntentFilter();
        filter.addAction(BluetoothDevice.ACTION_ACL_CONNECTED);
        filter.addAction(BluetoothAdapter.ACTION_STATE_CHANGED);
        context.registerReceiver(mBtConnReceiver, filter);
    }

    @Override
    public void teardown() {
        if (mContext != null) {
            mContext.unregisterReceiver(mBtConnReceiver);
            mContext = null;
        }
    }

    @Override
    public ConnectionThread<T> createConnectionThread(
            IConnectionThreadListener<T> connectionThreadListener) {
        if (mBluetoothAdapter.isEnabled()) {
            return new ConnectionThread<T>(mBluetoothPrepareTask, mPacketFactory,
                    connectionThreadListener, true);
        } else {
            return null;
        }
    }
}




Java Source Code List

net.cattaka.libgeppa.ActiveGeppaService.java
net.cattaka.libgeppa.AdkPassiveGeppaService.java
net.cattaka.libgeppa.BluetoothPassiveGeppaService.java
net.cattaka.libgeppa.Constants.java
net.cattaka.libgeppa.GeppaService.java
net.cattaka.libgeppa.IRawSocket.java
net.cattaka.libgeppa.PassiveGeppaService.java
net.cattaka.libgeppa.adapter.AbsConnectionAdapter.java
net.cattaka.libgeppa.adapter.DummyDeviceAdapter.java
net.cattaka.libgeppa.adapter.IDeviceAdapterListener.java
net.cattaka.libgeppa.adapter.IDeviceAdapter.java
net.cattaka.libgeppa.adapter.IDeviceCommandAdapter.java
net.cattaka.libgeppa.adapter.LocalDeviceAdapter.java
net.cattaka.libgeppa.adapter.RemoteDeviceAdapter.java
net.cattaka.libgeppa.binder.ActiveGeppaServiceFuncs.java
net.cattaka.libgeppa.binder.PassiveGeppaServiceFuncs.java
net.cattaka.libgeppa.binder.async.ActiveGeppaServiceFuncsAsync.java
net.cattaka.libgeppa.binder.async.AsyncInterfaceException.java
net.cattaka.libgeppa.binder.async.PassiveGeppaServiceFuncsAsync.java
net.cattaka.libgeppa.bluetooth.BluetoothAdapterFactory.java
net.cattaka.libgeppa.bluetooth.BluetoothAdapterWrapper.java
net.cattaka.libgeppa.bluetooth.BluetoothDeviceWrapper.java
net.cattaka.libgeppa.bluetooth.BluetoothSocketWrapper.java
net.cattaka.libgeppa.bluetooth.IBluetoothAdapter.java
net.cattaka.libgeppa.bluetooth.IBluetoothDevice.java
net.cattaka.libgeppa.bluetooth.IBluetoothSocket.java
net.cattaka.libgeppa.data.BaudRate.java
net.cattaka.libgeppa.data.ConnectionCode.java
net.cattaka.libgeppa.data.ConnectionState.java
net.cattaka.libgeppa.data.DeviceEventCode.java
net.cattaka.libgeppa.data.DeviceInfo.java
net.cattaka.libgeppa.data.DeviceState.java
net.cattaka.libgeppa.data.IPacketFactory.java
net.cattaka.libgeppa.data.IPacket.java
net.cattaka.libgeppa.data.PacketWrapper.java
net.cattaka.libgeppa.data.SocketState.java
net.cattaka.libgeppa.exception.NotImplementedException.java
net.cattaka.libgeppa.net.DummySocketPrepareTask.java
net.cattaka.libgeppa.net.DummySocket.java
net.cattaka.libgeppa.net.PhysicaloidSocketPrepareTask.java
net.cattaka.libgeppa.net.PhysicaloidSocket.java
net.cattaka.libgeppa.net.RemoteSocketPrepareTask.java
net.cattaka.libgeppa.net.RemoteSocket.java
net.cattaka.libgeppa.net.UsbClass.java
net.cattaka.libgeppa.passive.AdkPassiveReceiver.java
net.cattaka.libgeppa.passive.BluetoothPassiveReceiver.java
net.cattaka.libgeppa.passive.IPassiveReceiverListener.java
net.cattaka.libgeppa.passive.IPassiveReceiver.java
net.cattaka.libgeppa.socket.AdkRawSocket.java
net.cattaka.libgeppa.socket.BtRawSocket.java
net.cattaka.libgeppa.thread.ClientThread.java
net.cattaka.libgeppa.thread.ConnectionThread.java
net.cattaka.libgeppa.thread.IConnectionThreadListener.java
net.cattaka.libgeppa.thread.ReceiveThread.java
net.cattaka.libgeppa.thread.ServerThread.java
net.cattaka.libgeppa.util.AidlUtil.java
net.cattaka.libgeppa.util.DeviceUtil.java