Android Example Send Text File using BlueTooth

I needed to send a text file created by my program to a users PC using Bluetooth.
Not finding anything exactly like this in the B4A forums I searched the web and came across this article: http://www.javacodegeeks.com/2013/09/bluetooth-data-transfer-with-android.html

So I coded this (I am including all the necessary code to make this compile)
B4X:
'-------------------------------------------------------------------------------------
'  The following routine  CreatUri  was found in one of the 
'         threads on the B4A forums  -  Thank you so much
'-------------------------------------------------------------------------------------
Private Sub CreateUri(uri As String) As Object
    
    Dim r As Reflector

    Return r.RunStaticMethod("android.net.Uri", "parse", Array As Object(uri), Array As String("java.lang.String"))            
	
End Sub

'---------------------------------------------------------------------------------
'  Code a created based on Article
'---------------------------------------------------------------------------------
Sub SendBTFile
   Dim gDirectoryBT     As String   = "/BowlingBrackets/BlueTooth/"

    Dim gFileNameBTOut  As String   = "BBBTFile2PC.txt"

    Dim I                   As Int

    Dim PM              As PackageManager	

    Dim PMList         As List
    Dim PMEntry      As String

    Dim BTIntent       As Intent

    BTIntent.Initialize("android.bluetooth.adapter.action.SEND", "")

    BTIntent.Action = BTIntent.ACTION_SEND
    BTIntent.Flags   = 1                                                                       'Set Flag  FLAG_GRANT_READ_URI_PERMISSION  

    BTIntent.SetType("text/plain")
    BTIntent.PutExtra("android.intent.extra.STREAM", CreateUri("file:///" _
                                  &File.Combine(File.DirRootExternal, gDirectoryBT  &gFileNameBTOut)))

    '-----------------------------------------------------------------
    ' Look for Bluetooth interface
    '-----------------------------------------------------------------
    PMList.Initialize
    PMList = PM.QueryIntentActivities(BTIntent)
			
    For i = 0 To PMList.Size-1
           PMEntry = PMList.Get(i)
				
           If PMEntry.Contains("com.android.bluetooth") == True Then
               BTIntent.SetComponent(PMEntry)
				   				   
               StartActivity(BTIntent)				   			   
               Return
            End If
    Next
End Sub

This code finds then Bluetooth interface and sets the proper fields (in Intent) needed and Starts the Intent activity

The user is presented (by the Bluetooth software) a list of Paired devices (wish I could find and pass that as well) and when the user selects a paired device it sends the file to the PC.

On a PC running Windows 8.1 the file is delivered to the users "Documents" folder in the sub directory "Bluetooth Exchange Folder".

Hope this helps anyone that needs to send a Text file to a PC


BobVal
 

Robert Valentino

Well-Known Member
Licensed User
Longtime User
Interesting ("///" vs "//") was having troubles in the beginning and I thought the "///" fixed the problem, but I just tried it both ways and it works both ways, I would have thought one of the ways would fail.

Thanks for the info on ContentResolver (still learning all the ways of doing things)
 
Top