ContactsFragment now lists all visible devices.
Also added ChatService to handle discovery (only on service start for now).
This commit is contained in:
parent
63c58a780e
commit
199f2e703d
5 changed files with 155 additions and 3 deletions
|
@ -0,0 +1,72 @@
|
|||
package com.nutomic.ensichat.bluetooth
|
||||
|
||||
import android.app.Service
|
||||
import android.bluetooth.{BluetoothDevice, BluetoothAdapter}
|
||||
import android.content.{Context, BroadcastReceiver, IntentFilter, Intent}
|
||||
import android.os.IBinder
|
||||
import android.util.Log
|
||||
import com.nutomic.ensichat.bluetooth.ChatService.DeviceListener
|
||||
|
||||
object ChatService {
|
||||
trait DeviceListener {
|
||||
def onDeviceConnected(device: Device): Unit
|
||||
}
|
||||
}
|
||||
|
||||
class ChatService extends Service {
|
||||
|
||||
private val TAG = "ChatService"
|
||||
|
||||
private final val mBinder = new ChatServiceBinder(this)
|
||||
|
||||
private var mBluetoothAdapter: BluetoothAdapter = _
|
||||
|
||||
private var mDeviceListener: DeviceListener = _
|
||||
|
||||
override def onCreate(): Unit = {
|
||||
super.onCreate()
|
||||
|
||||
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter()
|
||||
|
||||
var filter: IntentFilter = new IntentFilter(BluetoothDevice.ACTION_FOUND)
|
||||
registerReceiver(mReceiver, filter)
|
||||
filter = new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_FINISHED)
|
||||
registerReceiver(mReceiver, filter)
|
||||
doDiscovery()
|
||||
}
|
||||
|
||||
override def onBind(intent: Intent): IBinder = {
|
||||
return mBinder
|
||||
}
|
||||
|
||||
override def onDestroy(): Unit = {
|
||||
super.onDestroy()
|
||||
unregisterReceiver(mReceiver)
|
||||
}
|
||||
|
||||
def doDiscovery() {
|
||||
// If we're already discovering, stop it.
|
||||
if (mBluetoothAdapter.isDiscovering()) {
|
||||
mBluetoothAdapter.cancelDiscovery()
|
||||
}
|
||||
|
||||
mBluetoothAdapter.startDiscovery()
|
||||
Log.i(TAG, "Discovery started")
|
||||
}
|
||||
|
||||
private final def mReceiver: BroadcastReceiver = new BroadcastReceiver() {
|
||||
override def onReceive(context: Context, intent: Intent) {
|
||||
intent.getAction() match {
|
||||
case BluetoothDevice.ACTION_FOUND =>
|
||||
val btDevice: BluetoothDevice = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE)
|
||||
mDeviceListener.onDeviceConnected(new Device(btDevice))
|
||||
case _ =>
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
def registerDeviceListener(listener: DeviceListener): Unit = {
|
||||
mDeviceListener = listener
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,11 @@
|
|||
package com.nutomic.ensichat.bluetooth
|
||||
|
||||
import android.os.Binder
|
||||
|
||||
class ChatServiceBinder (mService: ChatService) extends Binder {
|
||||
|
||||
def getService(): ChatService = {
|
||||
return mService
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,9 @@
|
|||
package com.nutomic.ensichat.bluetooth
|
||||
|
||||
import android.bluetooth.BluetoothDevice
|
||||
|
||||
class Device(mBluetoothDevice: BluetoothDevice) {
|
||||
|
||||
def name = mBluetoothDevice.getName()
|
||||
|
||||
}
|
|
@ -1,18 +1,61 @@
|
|||
package com.nutomic.ensichat.fragments
|
||||
|
||||
import android.app.Fragment
|
||||
import android.os.Bundle
|
||||
import android.content.{Context, Intent, ComponentName, ServiceConnection}
|
||||
import android.os.{IBinder, Bundle}
|
||||
import android.view.LayoutInflater
|
||||
import android.view.View
|
||||
import android.view.ViewGroup
|
||||
import android.widget.{ArrayAdapter, ListView}
|
||||
|
||||
import com.nutomic.ensichat.R
|
||||
import com.nutomic.ensichat.bluetooth.ChatService.DeviceListener
|
||||
import com.nutomic.ensichat.bluetooth.{ChatServiceBinder, ChatService, Device}
|
||||
import com.nutomic.ensichat.util.DevicesAdapter
|
||||
|
||||
class ContactsFragment extends Fragment {
|
||||
class ContactsFragment extends Fragment with DeviceListener {
|
||||
|
||||
private var mListView: ListView = _
|
||||
|
||||
private var mChatService: ChatService = _
|
||||
|
||||
private final val mChatServiceConnection: ServiceConnection = new ServiceConnection {
|
||||
override def onServiceConnected(componentName: ComponentName, iBinder: IBinder): Unit = {
|
||||
val binder: ChatServiceBinder = iBinder.asInstanceOf[ChatServiceBinder]
|
||||
mChatService = binder.getService()
|
||||
mChatService.registerDeviceListener(ContactsFragment.this)
|
||||
}
|
||||
|
||||
override def onServiceDisconnected(componentName: ComponentName): Unit = {
|
||||
mChatService = null
|
||||
}
|
||||
}
|
||||
|
||||
private var mAdapter: ArrayAdapter[Device] = _
|
||||
|
||||
override def onCreateView(inflater: LayoutInflater, container: ViewGroup,
|
||||
savedInstanceState: Bundle): View = {
|
||||
return inflater.inflate(R.layout.fragment_contacts, container, false)
|
||||
val view: View = inflater.inflate(R.layout.fragment_contacts, container, false)
|
||||
mListView = view.findViewById(android.R.id.list).asInstanceOf[ListView]
|
||||
mAdapter = new DevicesAdapter(getActivity)
|
||||
mListView.setAdapter(mAdapter)
|
||||
return view
|
||||
}
|
||||
|
||||
override def onCreate(savedInstanceState: Bundle): Unit = {
|
||||
super.onCreate(savedInstanceState)
|
||||
getActivity.startService(new Intent(getActivity, classOf[ChatService]))
|
||||
getActivity.bindService(new Intent(getActivity, classOf[ChatService]),
|
||||
mChatServiceConnection, Context.BIND_AUTO_CREATE)
|
||||
}
|
||||
|
||||
override def onDestroy(): Unit = {
|
||||
super.onDestroy()
|
||||
getActivity.unbindService(mChatServiceConnection)
|
||||
}
|
||||
|
||||
override def onDeviceConnected(device: Device): Unit = {
|
||||
mAdapter.add(device)
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -0,0 +1,17 @@
|
|||
package com.nutomic.ensichat.util
|
||||
|
||||
import android.content.Context
|
||||
import android.view.{ViewGroup, View}
|
||||
import android.widget.{TextView, ArrayAdapter}
|
||||
import com.nutomic.ensichat.bluetooth.Device
|
||||
|
||||
class DevicesAdapter(context: Context) extends ArrayAdapter[Device](context, android.R.layout.simple_list_item_1) {
|
||||
|
||||
override def getView(position: Int, convertView: View, parent: ViewGroup): View = {
|
||||
val view = super.getView(position, convertView, parent)
|
||||
val title: TextView = view.findViewById(android.R.id.text1).asInstanceOf[TextView]
|
||||
title.setText(getItem(position).name)
|
||||
return view
|
||||
}
|
||||
|
||||
}
|
Reference in a new issue