Bluetooth chat in java

Saved searches

Use saved searches to filter your results more quickly

You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session.

Messaging over Bluetooth (Android)

License

glodanif/BluetoothChat

This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?

Sign In Required

Please sign in to use Codespaces.

Launching GitHub Desktop

If nothing happens, download GitHub Desktop and try again.

Launching GitHub Desktop

If nothing happens, download GitHub Desktop and try again.

Launching Xcode

If nothing happens, download Xcode and try again.

Launching Visual Studio Code

Your codespace will open once ready.

There was a problem preparing your codespace, please try again.

Latest commit

Git stats

Files

Failed to load latest commit information.

README.md

1-to-1 chatting app over Bluetooth

This project is a test area for trying and mastering fancy programming stuff: Kotlin, MVP, DI, Coroutines, testing, Architecture Components, the newest Android features.

You can help to localize Bluetooth Chat to your language using Crowdin

Copyright 2017 Vasyl Glodan Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. 

About

Messaging over Bluetooth (Android)

Источник

Saved searches

Use saved searches to filter your results more quickly

You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session.

Example bluetooth chat app using Bluetooth LE library to communicate between android devices

License

niedev/BluetoothCommunicatorExample

This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?

Sign In Required

Please sign in to use Codespaces.

Launching GitHub Desktop

If nothing happens, download GitHub Desktop and try again.

Launching GitHub Desktop

If nothing happens, download GitHub Desktop and try again.

Launching Xcode

If nothing happens, download Xcode and try again.

Launching Visual Studio Code

Your codespace will open once ready.

There was a problem preparing your codespace, please try again.

Latest commit

Git stats

Files

Failed to load latest commit information.

README.md

This repository contains an open source sample application of the BluetoothCommunicator library.
The library, using Bluetooth Low Energy, allows you to communicate in P2P mode between two or more android devices in a very simple way.

The application is a Bluetooth chat, when the app is open in the first screen (search) it discovers nearby devices with the app open in the same search screen and shows them in a list containing the code number name of the phones (the name is customizable in the library, but not in the app).

Читайте также:  K nearest neighbors python sklearn

When the user clicks on one of the names the app sends a connection request to that phone and if the latter accepts it, both apps start the chat screen where the two devices can send text messages (the library can send also raw data) to each other.

When one of the user press the back button the connection stops and the apps will return to the search screen.

If you want to see the demo app in action you can download it from here.

BluetoothCommunicator is a library originally created for RTranslator but can be used in any more generic case where a P2P communication system is needed between two or more android devices (approximately up to 4 with a direct connection between all devices, even more with a star structure), for an example app see this repository or RTranslator.

BluetoothCommunicator automatically implements (they are active by default):

  • reconnection in case of temporary connection loss.
  • reliable message sending:
    • splitting and rebuilding of long messages.
    • sending messages with a queue in order to always send the messages even in case of connection problems (they will be sent as soon as the connection is restored) and in the right order.

    For use the library in a project you have to add jitpack.io to your root build.gradle (project):

    Then add the last version of BluetoothCommunicator to your app build.gradle

    To use this library add these permissions to your manifest:

    If you need to use bluetooth advertising or search in background you need to add also the following permission:

    Then add android:largeHeap=»true» to the application tag in the manifest:
    Example

    After the installation of the library and the changes to the manifest is time to write the code: create a bluetooth communicator object, it is the object that handles all operations of bluetooth low energy library, if you want to manage the bluetooth connections in multiple activities I suggest you to save this object as an attribute of a custom class that extends Application and create a getter so you can access to bluetoothCommunicator from any activity or service with:

    ((custom class name) getApplication()).getBluetoothCommunicator(); 

    Next step is to initialize bluetoothCommunicator, the parameters are: a context, the name by which the other devices will see us (limited to 18 characters and can be only characters listed in BluetoothTools.getSupportedUTFCharacters(context) because the number of bytes for advertising beacon is limited) and the strategy (for now the only supported strategy is BluetoothCommunicator.STRATEGY_P2P_WITH_RECONNECTION)

    bluetoothCommunicator = new BluetoothCommunicator(this, "device name", BluetoothCommunicator.STRATEGY_P2P_WITH_RECONNECTION); 

    Then add the bluetooth communicator callback, the callback will listen for all events of bluetooth communicator:

    bluetoothCommunicator.addCallback(new BluetoothCommunicator.Callback() < @Override public void onBluetoothLeNotSupported() < super.onBluetoothLeNotSupported(); Notify that bluetooth low energy is not compatible with this device >@Override public void onAdvertiseStarted() < super.onAdvertiseStarted(); Notify that advertise has started, if you want to do something after the start of advertising do it here, because after startAdvertise there is no guarantee that advertise is really started (it is delayed) >@Override public void onDiscoveryStarted() < super.onDiscoveryStarted(); Notify that discovery has started, if you want to do something after the start of discovery do it here, because after startDiscovery there is no guarantee that discovery is really started (it is delayed) >@Override public void onAdvertiseStopped() < super.onAdvertiseStopped(); Notify that advertise has stopped, if you want to do something after the stop of advertising do it here, because after stopAdvertising there is no guarantee that advertise is really stopped (it is delayed) >@Override public void onDiscoveryStopped() < super.onDiscoveryStopped(); Notify that discovery has stopped, if you want to do something after the stop of discovery do it here, because after stopDiscovery there is no guarantee that discovery is really stopped (it is delayed) >@Override public void onPeerFound(Peer peer) < super.onPeerFound(peer); Here for example you can save peer in a list or anywhere you want and when the user choose a peer you can call bluetoothCommunicator.connect(peer founded) but if you want to use a peer for connect you have to have peer updated (see onPeerUpdated or onPeerLost), if you use a non updated peer the connection might fail instead if you want to immediate connect where peer is found you can call bluetoothCommunicator.connect(peer) here >@Override public void onPeerLost(Peer peer) < super.onPeerLost(peer); It means that a peer is out of range or has interrupted the advertise, here you can delete the peer lost from a eventual collection of founded peers >@Override public void onPeerUpdated(Peer peer,Peer newPeer) < super.onPeerUpdated(peer,newPeer); It means that a founded peer (or connected peer) has changed (name or address or other things), if you have a collection of founded peers, you need to replace peer with newPeer if you want to connect successfully to that peer. In case the peer updated is connected and you have saved connected peers you have to update the peer if you want to successfully send a message or a disconnection request to that peer. >@Override public void onConnectionRequest(Peer peer) < super.onConnectionRequest(peer); It means you have received a connection request from another device (peer) (that have called connect) for accept the connection request and start connection call bluetoothCommunicator.acceptConnection(peer); for refusing call bluetoothCommunicator.rejectConnection(peer); (the peer must be the peer argument of onConnectionRequest) >@Override public void onConnectionSuccess(Peer peer,int source) < super.onConnectionSuccess(peer,source); This means that you have accepted the connection request using acceptConnection or the other device has accepted your connection request and the connection is complete, from now on you can send messages or data (or disconnection request) to this peer until onDisconnected To send messages to all connected peers you need to create a message with a context, a header, represented by a single character string (you can use a header to distinguish between different types of messages, or you can ignore it and use a random character), the text of the message, or a series of bytes if you want to send any kind of data and the peer you want to send the message to (must be connected to avoid errors), example: new Message(context,"a","hello world",peer); If you want to send message to a specific peer you have to set the sender of the message with the corresponding peer. To send disconnection request to connected peer you need to call bluetoothCommunicator.disconnect(peer); >@Override public void onConnectionFailed(Peer peer,int errorCode) < super.onConnectionFailed(peer,errorCode); This means that your connection request is rejected or has other problems, to know the cause of the failure see errorCode (BluetoothCommunicator.CONNECTION_REJECTED means rejected connection and BluetoothCommunicator.ERROR means generic error) >@Override public void onConnectionLost(Peer peer) < super.onConnectionLost(peer); This means that a connected peer has lost the connection with you and the library is trying to restore it, in this case you can update the gui to notify this problem. You can still send messages in this situation, all sent messages are put in a queue and sent as soon as the connection is restored >@Override public void onConnectionResumed(Peer peer) < super.onConnectionResumed(peer); Means that connection lost is resumed successfully >@Override public void onMessageReceived(Message message,int source) < super.onMessageReceived(message,source); Means that you have received a message containing TEXT, for know the sender you can call message.getSender() that return the peer that have sent the message, you can ignore source, it indicate only if you have received the message as client or as server >@Override public void onDataReceived(Message data,int source) < super.onDataReceived(data,source); Means that you have received a message containing DATA, for know the sender you can call message.getSender() that return the peer that have sent the message, you can ignore source, it indicate only if you have received the message as client or as server >@Override public void onDisconnected(Peer peer,int peersLeft) < super.onDisconnected(peer,peersLeft); Means that the peer is disconnected, peersLeft indicate the number of connected peers remained >@Override public void onDisconnectionFailed() < super.onDisconnectionFailed(); Means that a disconnection is failed, super.onDisconnectionFailed will reactivate bluetooth for forcing disconnection (however the disconnection will be notified in onDisconnection) >>); 

    Finally you can start discovery and/or advertising:

    bluetoothCommunicator.startAdvertising(); bluetoothCommunicator.startDiscovery(); 

    All other actions that can be done are explained with the comments in the code of callback I wrote before. For more details see the code of this example app

    For anyone who wants to examinate the library code and generate the .aar file after clone the library on Android Studio: click on the «Gradle» tab in the right edge of Android Studio, then click on BluetoothCommunicator -> app -> Task -> build -> assemble, then go to the local folder of the BluetoothCommunicator project and click on app -> build -> outputs -> aar, here will be the debug and release .aar files

    Avoid to have installed on your phone multiple apps that use this library, because in that case the bluetooth connection will have problems (maybe it is due to the fact that they are running advertising with the same UUID, try downloading the source files and changing the advertising UUID in the code if you want to try to fix). In case you have multiple apps using this library, uninstall all but one of them and restart your device in case of problems.

    This is an open source project, I don’t make any money from it.

    So, if you found this project useful and want to say thank you and support it, you can make a donation via paypal by clicking on the button below (any amount is well accepted).

    Donate

    In case you will donate, thank you ❤️

    About

    Example bluetooth chat app using Bluetooth LE library to communicate between android devices

    Источник

    Saved searches

    Use saved searches to filter your results more quickly

    You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session.

    Android Bluetooth Chat 💬 App 📱

    halilozel1903/AndroidBluetoothChatApp

    This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

    Name already in use

    A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?

    Sign In Required

    Please sign in to use Codespaces.

    Launching GitHub Desktop

    If nothing happens, download GitHub Desktop and try again.

    Launching GitHub Desktop

    If nothing happens, download GitHub Desktop and try again.

    Launching Xcode

    If nothing happens, download Xcode and try again.

    Launching Visual Studio Code

    Your codespace will open once ready.

    There was a problem preparing your codespace, please try again.

    Latest commit

    Git stats

    Files

    Failed to load latest commit information.

    README.md

    Android Bluetooth 🌐 Chat 💬 App 📱

    Android Bluetooth Chat application is an application written in Java programming language. It’s a project created by synchronizing with chat to learn the Bluetooth structure.

    Screenshot

    Bluetooth is a wireless communication technology that enables the wireless transmission of data over short distances. Bluetooth can communicate with 2.4 GHz using radio frequencies. Bluetooth technology can reach up to 10 meters of shooting capacity in an open area. Data transfer speeds between 1 Mbps and 721 Kbps are achieved in transfers with Bluetooth.

    1. Android OS supports networking via Bluetooth. It transfers data between devices using wireless communication.
    2. By using Android Bluetooth APIs, we can easily access Bluetooth related functions.
    3. It allows to connect to other devices using the Bluetooth API.

    What can we do using Bluetooth APIs ❓

    Screenshot

    • Scan for other Bluetooth devices
    • Questioning Bluetooth devices paired with the device
    • Transferring data with other devices
    • Connecting to other devices with services
    • Managing multiple wireless connections

    Screenshot

    Screenshot

    If this project help 💁 you to develop, you can give me a cup of coffee. ☕

    Bluetooth chat in java

    MIT License Copyright (c) 2022 Halil OZEL Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 

    About

    Android Bluetooth Chat 💬 App 📱

    Источник

Оцените статью